【问题标题】:Tkinter - why I must click the cross 2 times before the window has been closedTkinter - 为什么我必须在窗口关闭之前点击十字 2 次
【发布时间】:2017-02-28 18:01:35
【问题描述】:

当我创建subwindow (toplevel) 时,我无法一键关闭主窗口。我必须单击交叉并确定尽可能多的times(+1),就像我调用的函数一样:

import tkMessageBox
from Tkinter import *


def cross():
    if tkMessageBox.askokcancel(u"Quit ATTA Convertor ?", "Are you sure you want to close the window ?"):
        mainwin.quit()

def about():
    subwin = Toplevel(mainwin)
    subwin.mainloop()

mainwin = Tk()
mainwin.protocol("WM_DELETE_WINDOW", cross)

mainMenu = Menu(mainwin)
mainMenu.add_command(label="about...", command=about)
mainwin.config(menu=mainMenu)

mainwin.mainloop()

【问题讨论】:

    标签: tkinter toplevel tkmessagebox


    【解决方案1】:

    这是因为你已经给mainloop() 打了两次电话。删除对subwin.mainloop() 的调用,您不需要它。一个合适的 tkinter 程序应该只调用一次 mainloop()

    【讨论】:

      【解决方案2】:

      您正在运行 2 个主循环事件。您应该删除 about() 方法中的那个。

      def about():
          subwin = Toplevel(mainwin)
          #subwin.mainloop() <- Remove this
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-06
        • 1970-01-01
        • 2014-02-19
        • 2022-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多