문제

I have created a script in Python which notifies me at a given event. 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. 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. That will process all pending Tk/X window events.

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

다른 팁

You have to call root.mainloop() to enable the program to respond to events.

One problem on your code is that you create a new Tk element each time you call the function window_warn. This might not be the cause of your issue, but creating multiple Tk elements is a bad practise that should be avoided. For instance, initialize the root element at the beginning and leave only the call to 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:

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

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:

    class Root(tk.Tk):
        def __init__(self):
            tk.Tk.__init__(self)
            ....
            class 1..
            class 2..
            self.mainloop()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top