我编写了这段代码,并意识到我无法通过单击'Close'
关闭GUI。它所做的就是在外壳程序窗口中创建>>>
。
我尝试过更改命令,但是因为这是我第一次使用GUI,所以非常简单。另一个问题是窗口的'title'
应该在顶部的各个句子之间改变,但不会循环。
from tkinter import Tk, Label, Button, StringVar
class MyFirstGUI:
LABEL_TEXT = [
"This is our first GUI!",
"Actually, this is our second GUI.",
"We made it more interesting...",
"...by making this label interactive.",
"Go on, click on it again.",
]
def __init__(self, master):
self.master = master
master.title("A simple GUI")
self.label_index = 0
self.label_text = StringVar()
self.label_text.set(self.LABEL_TEXT[self.label_index])
self.label = Label(master, textvariable=self.label_text)
self.label.bind("<Button-1>", self.cycle_label_text)
self.label.pack()
self.greet_button = Button(master, text="Greet", command=self.greet)
self.greet_button.pack()
self.close_button = Button(master, text="Close", command=master.quit)
self.close_button.pack()
def greet(self):
print("Greetings!")
def cycle_label_text(self, event):
self.label_index += 1
self.label_index %= len(self.LABEL_TEXT) #wrap around
self.label_text.set(self.LABEL_TEXT[self.label_index])
root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()