if-else条件查询中的GUI编程

时间:2016-08-17 08:22:05

标签: python-3.x

我目前正在学习python编程语言。我想知道如何在条件下调用GUI,例如Button或Radiobutton。

1 个答案:

答案 0 :(得分:0)

此示例可能会给您一些想法。尝试将变量'need_btn'设置为True和False

import tkinter as tk                                                 

class App(tk.Frame):                                                 
    def __init__(self, master=None, hibtn=True):                     
        tk.Frame.__init__(self, master)                              
        self.pack()                                                  
        self.createWidgets(hibtn=hibtn)                              

    def createWidgets(self, hibtn=True):                             
        if hibtn:                                                    
            self.btn = tk.Button(self)                               
            self.btn["text"] = "Hello\n(click me)"                   
            self.btn["command"] = self.say_hi                        
            self.btn.pack()                                          

        self.ext = tk.Button(self, text="Exit", command=root.destroy)
        self.ext.pack()                                              

    def say_hi(self):                                                
        print("hi from Tkinter!")                                    

root = tk.Tk()                                                       

need_btn = False                                                     
# need_btn = True                                                    

if need_btn:                                                         
    app = App(master=root)                                           
else:                                                                
    app = App(master=root, hibtn=False)                              

app.mainloop()