I’m building an tkinter app on Python 3.7 and creating an .exe with Pyinstaller 3.5 in Windoows 10. When running the code from the IDE, all intended keyboard commands work as expected. However, in the executable, key combinations do not work while single key presses are fine.
Here is some test code that demonstrates the problem:
import tkinter as tk root = tk.Tk() txt = tk.StringVar() lbl = tk.Label(root, textvariable=txt) def key_handle(event): global txt txt.set(event.keysym) def kc_handle(event): tk.messagebox.showinfo('Key Combo', 'Key Combo pressed') root.bind('<Key>', key_handle) root.bind('<Alt-b>', kc_handle) lbl.pack() root.mainloop()
Pyinstaller is then invoked as pyinstaller -w -F key_test.py
.
One thing I do know is that the order of the binds doesn’t seem to make a difference. How do I get key combinations working in the executable?
Advertisement
Answer
IDLE
is built using tkinter
and it could import all needed modules for own use and your code could work correctly but when you run it without IDLE
then you have to import all modules which you use in code. In your example it will be
import tk.messagebox
BTW: Often similar problem is with mainloop()
. IDLE
aready runs mainloop()
so code may work without own mainloop()
. But normally (without IDLE
) it needs to use mainloop()
. It is good to check code in terminal/console/cmd.exe
to see if it gives any errors.