创建一个弹出窗口以确认退出tkinter应用

时间:2018-08-04 23:19:29

标签: python tkinter

我想要一个“退出”按钮,当您按下它时,将弹出一个新窗口,询问您是否确定要退出。我只是刚刚开始使用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会创建一个新窗口,然后如果他们按no按钮,则只会破坏exitsure,但是如果他们选择yes按钮,它将退出所有内容。

3 个答案:

答案 0 :(得分:0)

这是一个最小的,可重复使用的示例,其中按下主应用程序上的QUIT按钮,将弹出一个窗口,要求您进行确认。

确认后,该应用已关闭;如果不是,则弹出窗口关闭,并且该应用程序保持活动状态。

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()

enter image description here

答案 1 :(得分:0)

#这是我设计的代码,可以很好地与我一起工作,希望也能解决您的问题。...

基本上,如果您按“是”,则使用quit命令,然后在“否”按钮中销毁同一消息窗口...

最好不要为小任务创建新功能

#  **************** 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()

enter image description here

#  ***************  main window ***********

mw=Tk()
mw.overrideredirect(1)
mw.geometry("300x100")
btn=Button(mw, text="click to close", bg="red", fg="white", width=10, command=quit_msg).pack(pady=30)
mw.mainloop()

enter image description here

答案 2 :(得分:0)

好的,我自己使用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()