我正在尝试将双击命令绑定到框架,但是我希望该命令绑定到框架内的所有内容,而不仅仅是框架本身(想绑定到框架的所有子元素)
这是我尝试将命令绑定到框架的方式
from tkinter import *
class CustomFrame(Frame):
def __init__(self, master, *args, **kwargs):
super(CustomFrame, self).__init__(master, *args, **kwargs)
label = Label(self, text="Click Me", bg="red")
label.place(width=50, height=50, x=25, y=25)
self.configure(width=100, height=100)
class window(Tk):
def __init__(self):
super(window, self).__init__()
self.geometry("200x200")
self.resizable(False, False)
frame = CustomFrame(self, bg="green")
frame.place(x=30, y=30)
frame.bind('<Double-Button-1>', lambda e: print("testing")) # binding the command here
if __name__ == '__main__':
window = window()
window.mainloop()
我该怎么做,以便将“单击我” Label
绑定到同一命令,所有子元素也都绑定。
答案 0 :(得分:0)
@HenryYik通过.winfo_children()
方法帮助回答了我自己的问题
如果创建了CustomFrame
,则可以对.bind_frame()
进行Frame
命令来绑定到CustomFrame
的所有子元素,如下所示:
class CustomFrame(Frame):
def __init__(self, master, *args, **kwargs):
super(CustomFrame, self).__init__(master, *args, **kwargs)
label = Label(self, text="Click Me", bg="red")
label.place(width=50, height=50, x=25, y=25)
self.configure(width=100, height=100)
def bind_frame(self, sequence=None, func=None, add=None):
self.bind(sequence, func, add)
for child in self.winfo_children():
child.bind(sequence, func, add)