这个问题在这里已有答案:
以下是我的控制面板代码,上面有8个按钮 .
from Tkinter import *
import ttk
class light_control(Frame):
def __init__(self,i,parent,root,ltext,btext):
Frame.__init__(self,parent)
self.lc_Label = Label(self,text=ltext).pack()
self.lc_Button = Button(self,
text=btext,
width=10,
command=lambda i=i:root.toggle_button(i)
).pack()
class mi_control_panel(Frame):
def __init__(self, master=Tk()):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.grid()
self.master.title("Control Panel")
self.pack(fill=BOTH, expand=1)
self.master.attributes('-zoomed', True)
self.state = True
self.master.attributes("-fullscreen", self.state)
self.light_controls = []
self.render_notebook()
def toggle_button(self,x):
print self.light_controls[x].lc_Button
def render_notebook(self):
n = ttk.Notebook(self)
f1 = ttk.Frame(n)
text2 = Label(f1,text='Control Panel for Lights',relief='ridge')
text2.grid(row=0,column=0,columnspan=4)
for x in xrange(8):
b = light_control(x,f1,self,"Light "+str(x),"OFF")
self.light_controls.append(b)
for i in xrange(1,3):
for j in xrange(4):
self.light_controls[4*i+j-4].grid(row=i,column=j,padx='80',pady='30')
n.add(f1, text='Lights')
n.grid(row=0,column=0,sticky='EW')
micp = mi_control_panel()
micp.tk.mainloop()
我的问题是,当我点击一个按钮时,我得到的只是无 . 这是调用toggle_button函数但我无法访问类light_control的变量lc_Button . 我打算做的是创建一个附加到同一回调函数的按钮列表,该列表标识单击了哪个按钮并相应地执行操作 . 我可以在toggle_button函数中打印变量x . 它工作正常 . 任何指南高度赞赏 . 提前致谢 .
1 回答
你的问题是
它分配给
pack()
返回的变量值,它总是None
你需要
顺便说一句:你和
Label
有同样的错误所以,如果您需要使用
self.lc_Label
则需要