[英]tkinter is creating extra buttons when selecting different radio options
我正在尝试制作一个多面板 GUI 来输入变量以运行回归树脚本。 我希望能够让 GUI 选择特定选项并将用户发送到特定面板进行填写。 我找到了一种方法来创建按钮 go 到不同的面板,基于选择哪个选项,但每次我更改 StartPage class 中的单选按钮选择时,它都会创建一个新按钮。
是否有更简单的方法来过滤将显示哪些面板,或者只是更新按钮的文本/命令?
import tkinter as tk
class ModelingGUI(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
subFrame = tk.Frame(self)
subFrame.pack(anchor='n')
Hyper = tk.Label(subFrame, text = "Run Parameter Optimization?")
Hyper.pack(side = 'left')
HParam = tk.StringVar()
HParam.set('PageOne')
def ParamChoice():
ParamChoice = tk.StringVar()
ParamChoice = HParam.get()
if ParamChoice =='PageOne':
NextButton1 = tk.Button(self, text = ParamChoice, command=lambda: controller.show_frame(PageOne))
NextButton1.pack()
if ParamChoice =='PageTwo':
NextButton2 = tk.Button(self, text = ParamChoice, command=lambda: controller.show_frame(PageTwo))
NextButton2.pack()
R2 = tk.Radiobutton(subFrame, text = 'No', variable=HParam, value='PageOne', command=ParamChoice)
R1 = tk.Radiobutton(subFrame, text = 'Yes', variable=HParam, value='PageTwo', command=ParamChoice)
R2.pack(side = 'right')
R1.pack(side = 'right')
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
WelcomeLabel = tk.Label(self,text = "Explicit Hyperparameter Settings")
WelcomeLabel.pack(side = 'top')
BackButton = tk.Button(self, text = 'Back',command=lambda: controller.show_frame(StartPage))
BackButton.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
WelcomeLabel = tk.Label(self,text = "Find Optimized Hyperparameters")
WelcomeLabel.pack(side = 'top')
BackButton = tk.Button(self, text = 'Back',command=lambda: controller.show_frame(StartPage))
BackButton.pack()
app = ModelingGUI()
app.mainloop()
我试过把.destoy() 放在
您应该创建一次按钮并在ParamChoice()
function 中更新其文本和命令选项:
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
subFrame = tk.Frame(self)
subFrame.pack(anchor='n')
Hyper = tk.Label(subFrame, text="Run Parameter Optimization?")
Hyper.pack(side='left')
HParam = tk.StringVar()
HParam.set('PageOne')
def ParamChoice():
choice = HParam.get()
if choice == 'PageOne':
NextButton.config(text=choice, command=lambda: controller.show_frame(PageOne))
elif choice == 'PageTwo':
NextButton.config(text=choice, command=lambda: controller.show_frame(PageTwo))
R2 = tk.Radiobutton(subFrame, text='No', variable=HParam, value='PageOne', command=ParamChoice)
R1 = tk.Radiobutton(subFrame, text='Yes', variable=HParam, value='PageTwo', command=ParamChoice)
NextButton = tk.Button(self) # create the button
R2.pack(side='right')
R1.pack(side='right')
NextButton.pack()
ParamChoice() # initialize the NextButton
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.