【发布时间】:2020-07-29 17:18:39
【问题描述】:
我正在尝试制作一个多面板 GUI,用于输入变量以运行回归树脚本。我希望能够让 GUI 选择特定选项并将用户发送到特定面板进行填写。我找到了一种方法来创建按钮以根据选择的选项转到不同的面板,但每次我更改 StartPage 类中的单选按钮选择时,它都会创建一个新按钮。
是否有更简单的方法来过滤将显示哪些面板,或者只更新按钮的文本/命令?
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() 放入
【问题讨论】:
标签: python tkinter radio-button