【问题标题】:tkinter is creating extra buttons when selecting different radio optionstkinter 在选择不同的单选选项时会创建额外的按钮
【发布时间】: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


    【解决方案1】:

    您应该创建一次按钮并在ParamChoice() 函数中更新其文本和命令选项:

    
    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 
    
    

    【讨论】:

    • 感谢您的帮助,已解决。现在我可以停止在这个问题上费尽心思了。
    猜你喜欢
    • 2017-07-24
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2019-01-06
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多