我正在使用Tkinter和canvas小部件创建一个界面,到目前为止,已经找到了我从其他问题和发布的答案中得到的问题的答案,但我对此感到难过.
我在创建GUI元素的类中有几个键盘绑定,并且在程序启动时它们都可以正常工作.绑定看起来像这样:
self.canvas.get_tk_widget().bind("<Control-o>",self.flash_open)
Run Code Online (Sandbox Code Playgroud)
并且在该类的__init__函数内.截至昨天,我初始化了这个类来启动程序,然后等待用户从菜单中选择打开,然后打开(除其他外)一个tkmessagebox
self.specfilename =askopenfilename(filetypes=[("spec", "")],initialdir= self.pathname)
Run Code Online (Sandbox Code Playgroud)
使用此文件名,我能够从特定文件类型中检索所需的变量名称(对问题无关紧要).今天我修改了__init__函数,在程序启动时调用open函数.由于在打开此文件之前无法执行任何其他操作,因此首先打开它是有意义的.选择文件并关闭Tkmessagebox后,根窗口处于活动状态,但键盘绑定都不起作用.我的功能仍然可以使用分配给它们的菜单/按钮,而不是绑定.我尝试将快捷方式绑定到root,结果相同,我现在认为这可能是我调用它们的顺序的问题
def __init__(self):
...
self.openfile() #calls the tkmessagebox
self.root.mainloop() #starts gui
Run Code Online (Sandbox Code Playgroud)
我之前遇到过这个问题,其中一个toplevel()实例被关闭/销毁并禁用了父窗口的绑定.没有任何错误信息可言,绑定只是不做任何事情.我还要提一下,我已经尝试过再次关注根窗口
self.openfile()
self.root.mainloop()
self.root.focus_set()
Run Code Online (Sandbox Code Playgroud)
之前我使用wm_withdraw()和wm_deiconify()函数来隐藏子窗口,然后在程序完成后关闭它.但是,在这种情况下,此修复更难以应用.如果有人能够解释问题的原因,我会很感激.
编辑:
我写了一个可运行的代码段来准确显示我的问题.
import os
from tkFileDialog import askopenfilename
from Tkinter import *
class Start:
def __init__(self):
self.root = Tk()
self.root.title('Binding Troubles')
menubar = Menu(self.root)
#add items and their commands to the menubar
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Do work", command=self.do_work)
filemenu.add_command(label="Open File",command=self.openfile)
menubar.add_cascade(label="File", menu=filemenu)
#bind control-o to perform the do work function
self.root.bind("<Control-o>",self.flash_do_work)
self.root.bind("<Control-O>",self.flash_do_work)
#add the menubar …Run Code Online (Sandbox Code Playgroud)