frame.destroy()和frame.quit()不起作用

时间:2017-08-01 16:35:59

标签: python python-3.x tkinter

我正在使用python 3我想关闭一个tkinter窗口继续代码,但它不起作用。 这是我的代码:

import tkinter as tk
from tkinter import *
from tkinter import messagebox

def window():
    global frame
    frame = Tk()

    Button1 = tk.Button(frame, text="No.", command=frame.quit)
    Button1.pack(anchor=S, fill=X, side=RIGHT)
    Button2 = tk.Button(frame, text="Yes!", fg="dark green", command=func)
    Button2.pack(anchor=S, fill=X, side=LEFT)
    frame.mainloop()
 def func():
    frame.destroy()
    frame.quit()
    messagebox.showinfo("Help", "Please help me.")
    #Next step ....

window()

我想用Button1退出脚本并继续使用Button2关闭窗口,但是我无法使用其他功能关闭窗口。

2 个答案:

答案 0 :(得分:0)

import tkinter as tk
from tkinter import messagebox


def window():
    global frame
    frame = tk.Tk()

    Button1 = tk.Button(frame, text="No.", command=func)
    Button1.pack(anchor=tk.S, fill=tk.X, side=tk.RIGHT)
    Button2 = tk.Button(frame, text="Yes!", fg="dark green", command=func)
    Button2.pack(anchor=tk.S, fill=tk.X, side=tk.LEFT)
    frame.mainloop()
def func():
    global frame
    frame.destroy()
    messagebox.showinfo("Help", "Please help me.")
    #Next step ....

window()

答案 1 :(得分:0)

我已经解决了, 问题是:Evry messagebox需要一个主窗口,我在启动按钮之前销毁了这个主窗口,并且消息框启动了一个新的emty主窗口。我的结果是最小化主窗口,启动消息框,然后关闭按钮盒以便在代码中进行cotinueing。

 from tkinter import *

 from tkinter import messagebox

main = Tk()
main.geometry("500x400+300+300")
def message():
  main.geometry("0x0")
  messagebox.showwarning("Say Hello", "Hello World")
  main.destroy()
B1 = Button(main, text = "Start Dialog",fg="dark green", command = message)
B1.pack()

main.mainloop()

print("finish dialog")