我目前正在学习python编程语言。我想知道如何在条件下调用GUI,例如Button或Radiobutton。
答案 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()