Python 创建一个弹出窗口以确认退出tkinter应用程序
我想有一个“退出”按钮,当你按下它时,会弹出一个新窗口,询问你是否确定要退出。我刚刚开始使用Python 创建一个弹出窗口以确认退出tkinter应用程序,python,tkinter,Python,Tkinter,我想有一个“退出”按钮,当你按下它时,会弹出一个新窗口,询问你是否确定要退出。我刚刚开始使用tkinter,所以我不知道如何继续:到目前为止,我的应用程序将退出屏幕覆盖在标题屏幕的顶部,并创建一个看似随机的新空白窗口 以下是我目前的代码: import tkinter as tk ##_______________EXIT_______________ def closeyes(): exit() def closeno(): exitsure.destroy() def
tkinter
,所以我不知道如何继续:到目前为止,我的应用程序将退出屏幕覆盖在标题屏幕的顶部,并创建一个看似随机的新空白窗口
以下是我目前的代码:
import tkinter as tk
##_______________EXIT_______________
def closeyes():
exit()
def closeno():
exitsure.destroy()
def close_window():
exitsure = tk.Tk()
areyousure = tk.Label(text="Are you sure you want to exit?")
areyousure.grid(column=0, row=0)
ExitYes = tk.Button(text="Yes", command = closeyes)
ExitYes.grid(column=0, row=2)
ExitNo = tk.Button(text="No", command = closeno)
ExitNo.grid(column=2, row=2)
exitsure.mainloop()
#_______________START_______________
start = tk.Tk()
start.title("THE MEGA POP QUIZ")
#Start Title
start_title = tk.Label(text="Welcome to THE MEGA POP QUIZ")
start_title.grid(column=0, row=1)
#Begin button
def BEGIN():
start.destroy()
Button1 = tk.Button(text="BEGIN", command = BEGIN)
Button1.grid(column=0, row=2)
#Exit
ExitButton = tk.Button(text="EXIT", width = 14, command = close_window)
ExitButton.grid(column=0, row=0)
start.mainloop()
我以为exitsure=tk.tk会创建一个新窗口,然后如果他们按下“否”按钮,它只会破坏exitsure,但如果他们选择“是”按钮,它会退出所有内容。下面是一个可重复使用的最小示例,其中按下主应用程序上的“退出”按钮,启动一个弹出窗口,要求您确认 确认后,应用程序关闭;否则,弹出窗口关闭,应用程序保持活动状态 这个
PopUpConfirmQuit
类可以像其他tkinter应用一样重用;它只需要主应用程序的退出按钮就可以启动它
import tkinter as tk
class PopUpConfirmQuit(tk.Toplevel):
"""A TopLevel popup that asks for confirmation that the user wants to quit.
.
Upon confirmation, the App is destroyed.
If not, the popup closes and no further action is taken
"""
def __init__(self, master=None):
super().__init__(master)
tk.Label(self, text="Are you sure you want to quit").pack()
tk.Button(self, text='confirm', command=master.destroy, fg='red').pack(side=tk.RIGHT, fill=tk.BOTH, padx=5, pady=5)
tk.Button(self, text='Nooooo!', command=self.destroy).pack(side=tk.RIGHT, fill=tk.BOTH, padx=5, pady=5)
class App(tk.Tk):
"""a minimal example App containing only a QUIT button, that launches
a confirmation popup window
"""
def __init__(self):
super().__init__()
self.quitbutton = tk.Button(self, text='QUIT', command=lambda: PopUpConfirmQuit(self))
self.quitbutton.pack()
self.mainloop()
App()
#这是我设计的代码,与我配合良好,我希望也能解决您的问题
基本上,如果你按“是”,然后使用“退出”命令,在“否”按钮中销毁相同的消息窗口
最好不要为小任务创建新函数
# **************** quit code ***********
from tkinter import *
def quit_msg():
qw=Tk()
frame1 = Frame(qw, highlightbackground="green", highlightcolor="green",highlightthickness=1, bd=0)
frame1.pack()
qw.overrideredirect(1)
qw.geometry("200x70+650+400")
lbl = Label(frame1, text="are you sure you want to quit")
lbl.pack()
yes_btn = Button(frame1, text="Yes", bg="light blue", fg="red",command=quit, width=10)
yes_btn.pack(padx=10, pady=10 , side=LEFT)
no_btn = Button(frame1, text="No", bg="light blue", fg="red",command=qw.destroy, width=10)
no_btn.pack(padx=10, pady=10, side=LEFT)
qw.mainloop()
好的,我刚刚用tk.Toplevel()自己修复了它,谢谢大家,下面是修复代码: 随机输入 将tkinter作为tk导入
##_____________EXIT_______________
def EXIT():
exitsure = tk.Toplevel()
areyousure = tk.Label(exitsure, text="Are you sure you want to exit?")
areyousure.grid(column=0, row=0)
ExitYes = tk.Button(exitsure, text="Yes", command=quit)
ExitYes.grid(column=0, row=2)
NoYes = tk.Button(exitsure, text="No", command=exitsure.destroy)
NoYes.grid(column=2, row=2)
#_____________START_______________
start = tk.Tk()
start.title("THE MEGA POP QUIZ")
#Start Title
start_title = tk.Label(text="Welcome to THE MEGA POP QUIZ")
start_title.grid(column=0, row=1)
#Begin button
def BEGIN():
start.destroy()
Button1 = tk.Button(text="BEGIN", command = BEGIN)
Button1.grid(column=0, row=2)
#Exit
ExitButton = tk.Button(text="EXIT", command = EXIT)
ExitButton.grid(column=0, row=0)
start.mainloop()
##_____________EXIT_______________
def EXIT():
exitsure = tk.Toplevel()
areyousure = tk.Label(exitsure, text="Are you sure you want to exit?")
areyousure.grid(column=0, row=0)
ExitYes = tk.Button(exitsure, text="Yes", command=quit)
ExitYes.grid(column=0, row=2)
NoYes = tk.Button(exitsure, text="No", command=exitsure.destroy)
NoYes.grid(column=2, row=2)
#_____________START_______________
start = tk.Tk()
start.title("THE MEGA POP QUIZ")
#Start Title
start_title = tk.Label(text="Welcome to THE MEGA POP QUIZ")
start_title.grid(column=0, row=1)
#Begin button
def BEGIN():
start.destroy()
Button1 = tk.Button(text="BEGIN", command = BEGIN)
Button1.grid(column=0, row=2)
#Exit
ExitButton = tk.Button(text="EXIT", command = EXIT)
ExitButton.grid(column=0, row=0)
start.mainloop()