我尝试使用tkinter模块在循环中创建绑定。在
from tkinter import *
class Gui(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def Arrays(self,rankings):
self.panels = {}
self.helpLf = LabelFrame(self, text="Array Layout:")
self.helpLf.grid(row=10, column=9, columnspan=5, rowspan=8)
for rows in range(10):
for cols in range(10):
x = str(rows)+"_"+str(cols)
self.row = Frame(self.helpLf)
self.bat = Button(self.helpLf,text=" ", bg = "white")
self.bat.grid(row=10+rows, column=cols)
self.panels[x] = self.bat
self.panels[x].bind('<Button-1>', self.make_lambda())
self.panels[x].bind('<Double-1>', self.color_change1)
def color_change(self, event):
"""Changes the button's color"""
self.bat.configure(bg = "green")
def color_change1(self, event):
self.bat.configure(bg = "red")
这里有10X10=100个按钮。活页夹只对最后一个按钮有效。有人知道我怎么用活页夹把所有的钮扣都贴上吗?在
您在
color_change1
中使用self.bat
,但是self.bat
保留了最后一个按钮,因为您在循环中重写了它。但您将所有按钮保留在self.panels[x]
中,以访问任何按钮。所以你可以用它。在但有一个更简单的解决方案-绑定使用变量
event
向执行的函数发送有关事件的一些信息。例如,event.widget
允许您访问执行函数color_change1
的小部件,以便您可以使用它:参见第Events and Bindings页的“事件属性”
相关问题 更多 >
编程相关推荐