我浏览了十几篇关于Tkinter、binding keypress事件和canvas小部件的帖子,但是我读到的解决方案都没有改变我的程序。GUI目前使用Button-1作为事件,但是我需要它能够从键盘操作。我试着用字母表中的各种键和字母。我还尝试了self.c.focus_set(),但当我尝试将事件绑定到画布或框架时,它不起作用。我的代码是:
from Tkinter import *
class App:
def __init__(self, master):
self.parent = master
frame = Frame(master)
screen_width, screen_height = master.winfo_screenwidth(), master.winfo_screenheight()
frame.configure(background='black')
frame.place(x=0,y=0,width=screen_width,height=screen_height)
master.overrideredirect(1)
h = .8*screen_height
self.c = Canvas(frame, width=h, height=h, bd=0, highlightthickness=0, bg='black')
self.c.place(relx=0.5, rely=0.5, anchor=CENTER)
radius = 10
self.c.update_idletasks()
hCanvas = (self.c.winfo_height())
offset = int(float(.4*hCanvas))
self.c.create_oval(hCanvas/2-radius, hCanvas/2-radius, hCanvas/2+radius, hCanvas/2+radius, fill='white', outline='white')
self.c.focus_set()
self.c.update_idletasks()
self.c.bind('<Key>', lambda event, arg=[radius]: self.create_stim(event, arg))
def create_stim(self, event, args):
print('create stim')
if __name__ == "__main__":
root = Tk()
root.attributes('-fullscreen', True)
app=App(root)
root.update()
root.mainloop()
提前感谢您的帮助!在
目前,当我以这种方式运行程序时,我不再是全屏的,我不能点击窗口来选择它,而且每次我在键盘上按一个键都会收到错误噪音。如果我把根.属性('-fullscreen',True)在app=app(root)之后,我在全屏显示GUI,我可以单击它,并且每次按一个键都会得到错误噪音。 我的电脑(MacBook Pro,运行OS X Yosemite,10.10.4)上运行的python版本(2.7.10)是否可能有问题?我应该下载Python3吗?在
这似乎是Tkinter在OSX上的一个限制。当您调用
master.overrideredirect(1)
时,它似乎关闭了tkinter处理键盘事件的功能。如果省略对overrideredirect
的调用,则代码将开始工作。在相关问题 更多 >
编程相关推荐