我已经搜索了解决方案并尝试实现它们,但没有成功让 tkinter小部件通过设置关键字参数Scale
来调用方法。command
我将小部件对象存储在字典的列表中。该方法在创建列表时调用,并且确实包含正确的对象。我认为范围可能是这里的问题。
import Tkinter as Tkinter
from Tkinter import *
import ttk as ttk
class App:
prefs = {
"Fred": "Dead", "Kims": "Limbs"
}
def __init__(self, root):
self.root = root
self.testiter=-1
lControls = {}
r=0 ; p=-1 ; self.pSlider = 0 ; self.pLabel = 0
for key, var in App.prefs.items():
lControls[key] = [] #add empty list for this key
p+=1
self.pSlider=p
s=Scale(root, from_=1, to=16, orient="horizontal",\
command= lambda x:self.UpdateValues(key)) #trying with 1 or no arguments
#s=Scale(root, from_=1, to=16, orient="horizontal",\
# command=self.UpdateValues(key))
#s=Scale(root, from_=1, to=16, orient="horizontal",\
# command=self.UpdateValues())
#s=Scale(root, from_=1, to=16, orient="horizontal",\
# command=self.UpdateValues) # <<< gives error "TypeError: UpdateValues() takes exactly 1 argument (2 given)"
#
lControls[key].append(s)
s.grid(row=r,column=0)
p+=1
self.pLabel=p
l=Label(root, text="AAArrrgggghhhH!")
lControls[key].append(l)
l.grid(row=r, column=1)
r+=1
def UpdateValues(self, key): #trying this with just self too
self.testiter+=1
print(self.testiter)
root = Tk()
App(root)
root.mainloop()