Reputation: 2790
On Python 3.6 and 3.7 on MacOS 10.12.6, a tkinter.Button
does not initially respond to clicks - but the same code works perfectly on Python 3.5.
If I run the following script:
import tkinter as tk
root = tk.Tk()
button = tk.Button(root, text='Button')
button.pack()
root.mainloop()
then on Python 3.6 and 3.7, a window appears with a single button as expected, it does not visually appear to be disabled, but clicking on it has no effect.
Resizing the window or clicking elsewhere within it does not fix the issue. However, if I bring another application to the foreground and then bring the Python application back to the front, everything works perfectly, and I am then unable to make the issue reappear.
In Python 3.5, running the test script brings up the application and the button is immediately responsive to clicks - as expected. Experimentation was unable to reproduce any issues in 3.5. The script also runs fine in Python 2.7 if tkinter
is replaced by Tkinter
.
Changing the pack
layout manager to grid
gave exactly the same results.
I went so far as to reboot the machine, with no change.
I am close to 100% certain that the Python 3.6 and Python 3.7 binaries and libraries are in a good state. They are essentially clean installs as I work entirely in virtualenvs. And my non-trivial tkinter
application runs identically on Python 3.5, 3.6 and 3.7, except for this one problem.
Upvotes: 2
Views: 83
Reputation: 15226
I do not have a Mac to test on but your issue sounds very similar to a focus problem that can occur on windows when you open a file dialog before the mainloop has had its first complete loop. The issue has the same symptoms like not allow focus to get back to the window until you bring up some other app and then go back to the tkinter app and then it works.
The way to correct the focus issue is to apply root.update_idletasks()
before the problem (IE. right before opening the file dialog) and in this case right before your button.
As the OP has pointed out they had to add the root.update_idletasks()
before and after the widget and I am not sure why this was the fix here for OSX. For those reading this with the same issue on Mac go ahead and try this option for a work around.
Upvotes: 3