Python Tkinter:一键移除并创建函数标签框架



选择第一个按钮后,此代码需要按下两次。1清除旧的标签框并重新配置按钮,2创建新框架。

你能一键使它发挥作用吗。

我正在一键切换帧。我只想一次打开相应按钮的框架。

from tkinter import *
root=Tk()
root.state('zoomed')
def remove_frame(lf):
lf.destroy()
a.config(command=A1)
b.config(command=A2)
c.config(command=A3)
def A1():
a.configure(relief=SUNKEN,state=DISABLED)
b.configure(relief=RAISED,state=ACTIVE)
c.configure(relief=RAISED,state=ACTIVE)
lf=LabelFrame(root,text='one')
lf.pack()
e=Button(lf,text='one')
e.pack()
b.config(command=lambda: remove_frame(lf))
c.config(command=lambda: remove_frame(lf))
def A2():
a.configure(relief=RAISED,state=ACTIVE)
b.configure(relief=SUNKEN,state=DISABLED)
c.configure(relief=RAISED,state=ACTIVE)
lf=LabelFrame(root,text='two')
lf.pack()
e=Button(lf,text='two')
e.pack()
a.config(command=lambda: remove_frame(lf))
c.config(command=lambda: remove_frame(lf))
def A3():
a.configure(relief=RAISED,state=ACTIVE)
b.configure(relief=RAISED,state=ACTIVE)
c.configure(relief=SUNKEN,state=DISABLED)
lf=LabelFrame(root,text='three')
lf.pack()
e=Button(lf,text='three')
e.pack()
a.config(command=lambda: remove_frame(lf))
b.config(command=lambda: remove_frame(lf))
l=LabelFrame(root,padx=30,pady=20)
l.pack(padx=0,pady=18)
a=Button(l,text="1",command=A1)
a.grid(row=0,column=1)
b=Button(l,text="2",command=A2)
b.grid(row=0,column=2)
c=Button(l,text="3",command=A3)
c.grid(row=0,column=3)
d=Button(l, text="4",command=root.quit)
d.grid(row=0,column=4)
mainloop()

pradepghr于2017年6月26日16:42 回答

"您可以像这样简单地使用lambda:"self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])

解决方案是停止remove_frame中的配置,并向原始函数添加多个绑定。

def remove_frame(lf):
lf.destroy()
# a.config(command=A1)
# b.config(command=A2)
# c.config(command=A3)
def A1():
a.config(relief=SUNKEN,state=DISABLED)
b.config(relief=RAISED,state=ACTIVE)
c.config(relief=RAISED,state=ACTIVE)
lf=LabelFrame(root,text='one')
lf.pack()
e=Button(lf,text='one')
e.pack()
b.config(command=lambda:[remove_frame(lf),A2()])
c.config(command=lambda:[remove_frame(lf),A3()])

最新更新