简体   繁体   English

单击“确定”后,Tkinter TkMessageBox未关闭

[英]Tkinter TkMessageBox not closing after click OK

I have created a script in Python which notifies me at a given event. 我用Python创建了一个脚本,它在给定的事件中通知我。 I am using the following function to produce the warning window: 我使用以下函数来生成警告窗口:

def window_warn():
    '''
    This function will throw up a window with some text
    '''
    #These two lines get rid of tk root window
    root = Tkinter.Tk()
    root.withdraw()
    #tkMessageBox.deiconify() 
    TkMessageBox.showwarning("New Case", "You have a new case\n Please restart pycheck")
    return

The window draws fine, but when I click ok, the window stays in place with the button depressed. 窗口绘制正常,但是当我单击确定时,窗口会在按下按钮的情况下保持原位。 Im using xfce. 我正在使用xfce。 Is there anyway to get the window to close after ok is clicked? 无论如何在点击确定后让窗口关闭?

A comment indicated this may be to do with surrounding code, so for completeness: 评论表明这可能与周围的代码有关,所以为了完整性:

print "Just started newcase check"
while True:
    if "Uncommitted" in webpage:
        print "oh look, 'Uncommitted' is in the url returned from the last function"
        #If this hits we call a notification window
        window_warn()
        print "sleeping"
        time.sleep(10)

        webpage = scrape_page()
    else:
        print "nothing"
        time.sleep(20)
        webpage = scrape_page()

Try calling root.update() before returning from the function. 在从函数返回之前尝试调用root.update() That will process all pending Tk/X window events. 这将处理所有待处理的Tk / X窗口事件。

(ideally, you'd establish a main event loop before displaying the window, but that assumes that your entire program is event driven, which may not always work.) (理想情况下,您在显示窗口之前建立一个主事件循环,但这假定您的整个程序是事件驱动的,这可能并不总是有效。)

您必须调用root.mainloop()以使程序能够响应事件。

One problem on your code is that you create a new Tk element each time you call the function window_warn . 您的代码的一个问题是每次调用函数window_warn时都会创建一个新的Tk元素。 This might not be the cause of your issue, but creating multiple Tk elements is a bad practise that should be avoided. 这可能不是您的问题的原因,但创建多个Tk元素是一种应该避免的不良做法。 For instance, initialize the root element at the beginning and leave only the call to showwarning : 例如,在开头初始化根元素,只保留对showwarning的调用:

root = Tkinter.Tk()
root.withdraw()

def window_warn():
    '''This function will throw up a window with some text'''
    tkMessageBox.showwarning("New Case", "You have a new case\n Please restart pycheck")
    return

print "Just started newcase check"
while True:
    # ...

I did it tis way: 我这样做了:

import Tkinter as tk
import tkMessageBox
root = tk.Tk()
root.withdraw()
t = tkMessageBox.askyesno ('Title','Are you sure?')
if t:
    print("Great!!!")
    root.update()
else:
    print("Why?")
    root.update()

Another solution is to track if the tk.messagebox has occurred, and if it has just break/continue/pass to skip over the re-occurring tk.messagebox : 另一个解决方案是跟踪tk.messagebox是否已经发生,以及它是否只是中断/继续/传递以跳过重新发生的tk.messagebox

Flag = False
if Flag: 
    messagebox.showerror("Error", "Your massage here.")
    Flag = True
else:
    break

I propose this because I had issues with other solutions proposed on StackOverflow as I don't have a dedicated root.mainloop() but only have self.mainloop() within the class Root() 我提出这个是因为我在StackOverflow上提出了其他解决方案的问题,因为我没有专用的root.mainloop()但只有类Root() self.mainloop() Root()

My root looks like this and the massage event is generated within some of the inner classes, where I have no access to self.root: 我的root看起来像这样,按摩事件是在一些内部类中生成的,我无法访问self.root:

    class Root(tk.Tk):
        def __init__(self):
            tk.Tk.__init__(self)
            ....
            class 1..
            class 2..
            self.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM