button text of tkinter not works in mojave

2019-03-13 08:10发布

It is very simple program.

import tkinter
tkinter.Button(None, text='button').pack()
tkinter.mainloop()

I expect this program shows me such as a below picture.
work well

But it shows a below picture actually.
not work

When it is resized on GUI it seems working well.

It didn't occur this problem in High Sierra.

My environment is following:

  • macOS Mojave 10.14
  • Python 3.7.0

Could you advice to me?

I found out same problem on here.

10条回答
Evening l夕情丶
2楼-- · 2019-03-13 08:20

I guess there is a bug in Tk. I am on MacOS 10.14.3 If you maximize and minimize the tkinter window the text on the button appears, another solution that worked for me is

from tkinter import *
from tkinter import ttk

button1 = tkk.Button(*your args here*)
查看更多
看我几分像从前
3楼-- · 2019-03-13 08:21

I have this problem with an app I wrote and froze with PyInstaller. It still works fine on PC and my Mac laptop that doesn't have Mojave, but on my desktop mac that recently updated to Mojave it has buttons without text, and some buttons are completely invisible until clicked on.

I found an easy solution on Reddit: just resize the window slightly, and the interface elements appear!

Link to Reddit thread

查看更多
一纸荒年 Trace。
4楼-- · 2019-03-13 08:25

I had this same exact error, and to get it fixed I had to change my buttons to ttk.Button and set a style. For example, add the following to import:

try: from tkinter import ttk # python 3
except: import ttk # python 2.7

And then after the root init:

    style = ttk.Style()
    style.map("C.TButton",
              foreground=[('pressed', 'red'), ('active', 'blue')],
              background=[('pressed', '!disabled', 'black'),
                          ('active', 'white')]
              )

Then when you are instantiating the Button:

self.button = ttk.Button(self, text="my cooool button",
                                 command=self.load_something_cool, style="C.TButton")

It worked perfectly to ensure that the text is displayed properly. Before I added the ttk bit, I was in the same boat as you in Mojave.

查看更多
贪生不怕死
5楼-- · 2019-03-13 08:25

For me it worked to update python to 3.5.6 from 3.5.4.

查看更多
Emotional °昔
6楼-- · 2019-03-13 08:26

Here's an example that patches up the problem for me (at least until the Python/Tkinter stuff gets cleaned up):

import tkinter
root = tkinter.Tk()
tkinter.Button(root, text='button').pack()
def fix():
    a = root.winfo_geometry().split('+')[0]
    b = a.split('x')
    w = int(b[0])
    h = int(b[1])
    root.geometry('%dx%d' % (w+1,h+1))
root.update()
root.after(0, fix)
tkinter.mainloop()

This was tested on macOS Version 10.14.2 (18C54) and Python 3.7.2 (loaded via Home-brew).

查看更多
仙女界的扛把子
7楼-- · 2019-03-13 08:27

I also had this problem, 100% reproducible on my Mac after upgrading to Mojave and when using Homebrew's python3.

Switching to Python.org 's Python 3.7.1 package download totally eliminated the problem for me.

查看更多
登录 后发表回答