我有一个代码,在文件的顶部我可以选择退出以关闭GUI。出于某种原因,当我点击"退出"它没有退出。我觉得在编写这段代码时我发现了一个简单的错误,但我似乎无法找到它。我希望另一组眼睛能够这样做。
#Import tkinter to make gui
from tkinter import *
from tkinter import ttk
#Makes the GUI the same way it was at the beginning of the program
def login(*args):
try:
if (len(name.get())>=2) and (len(pw.get())>=2):
name_entry.delete(0, END)
pw_entry.delete(0, END)
valid.set("")
else:
valid.set("Invalid!")
pass
except TypeError:
valid.set("Invalid!")
pass
except ValueError:
valid.set("Invalid!")
pass
#Sets title and creates gui
root = Tk()
root.title("Login")
topMenu=Menu(root)
root.config(menu=topMenu)
#Creates menu and submenus
subMenu=Menu(topMenu)
#Gives menu options
topMenu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="Exit", command=root.quit)
#Configures column and row settings and sets padding
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
name=StringVar()
pw=StringVar()
valid=StringVar()
#Widgets asking name and age
name_entry = ttk.Entry(mainframe, width=15, textvariable=name)
name_entry.grid(column=2, row=1, sticky=(W, E))
pw_entry = ttk.Entry(mainframe, width=15, textvariable=pw)
pw_entry.grid(column=2, row=2, sticky=(W, E))
ttk.Label(mainframe, text="Username").grid(column=1, row=1, sticky=(N, W))
ttk.Label(mainframe, text="Password").grid(column=1, row=2, sticky=(N,W))
#ttk.Label(mainframe, text=" ").grid(column=3, row=3, sticky=(E))
#Makes a button that clears everything and returns it to start of program
ttk.Button(mainframe, text="Login", width=30, command=login).grid(column=2, row=3, columnspan=2, sticky=(W, E))
ttk.Label(mainframe, textvariable=valid).grid(column=1, row=3, sticky=(E))
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
name_entry.focus()
root.bind('<Return>', login)
root.mainloop()
答案 0 :(得分:1)
&#34;尝试将root.quit更改为root.destroy以查看它是否更好。&#34; (布莱恩奥克利)
这设法做到了。我不知道为什么,但是root.quit在闲置时不会这样做。