Button text of tkinter does not work in mojave

▼魔方 西西 提交于 2019-11-30 02:03:48

Changing Appearance to Light Mode fixed this problem for me.

To change appearance, go to Settings -> General -> Appearance -> Light Mode.

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.

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 = ttk.Button(*your args here*)

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

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).

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.

I had this problem only when frozen using py2app. My fix was to use .update_idletasks() on the widget, after all elements are created for each frame or Toplevel.

Had the same issue with button text and dropdown text after packaging my program with Pyinstaller. I think its a Tk/Tcl bug. I was able to fix it by entering full screen (green button in grey bar lol) and then minimizing to normal size again (yellow button in grey bar lol).

The only viable fix I found for py2app Tkinter windows in Mojave was to update my python version to 2.7.15 (64bit not 64/32bit). Versions prior to this all displayed empty text in the py2app frozen binaries. Directly running the python code displayed fine. Inserting .update_idletasks() prior to the mainloop of each window didn't make a difference for me (tested on two Mojave machines).

I was having the same problem but now I have fixed it.

If you are using Anaconda, type the following commands in terminal and you should be good to go:

  1. conda update python
  2. conda update anaconda

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!