Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
898 views
in Technique[技术] by (71.8m points)

python - non blocking info dialog in Tkinter

I need a simple info box to display some status output, that I would alternatively dump to the console using print. The easiest possibility that I found is the following:

import Tkinter as tk
root = tk.Tk()
root.withdraw()

from tkMessageBox import showinfo    
showinfo('some caption', 'some info')

The only problem with this implementation is that my program (not written in Tkinter) will not continue running, until the 'ok' button of the showinfo messagebox is pressed. That is, the showinfo dialog will block.

Therefore my question: Is there a simple way to make showinfo non-blocking? Are there alternative messagebox implementations in Tkinter that are non-blocking? I can think of the typical use-cage of showing a help page - the window should open and the main program keeps on running normally.

EDIT1: here's a simple help window that I came up with, but unfortunately it doesn't show up, unless I launch a different tkMessageBox or a similar object:

class TextInfo(object):

    def __init__(self, parent, window_title = 'window', textfield = 'a text field', label = None):

        self.top = tk.Toplevel(parent)
        self.parent = parent
        self.window_title = window_title
        self.textfield = textfield

        # set window title
        if window_title:
            self.top.title(window_title)

        # add label if given
        if label:
            tk.Label(self.top, text=window_title).grid(row=0)

        # create the text field
        self.textField = tk.Text(self.top, width=80, height=20, wrap=tk.NONE)
        if textfield:
            self.textField.insert(1.0, textfield)
        self.textField.grid(row=1)

        # create the ok button
        b = tk.Button(self.top, text="OK", command=self.ok)
        b.grid(row=2)

    def ok(self):
        self.top.destroy()

And this is how I call the window:

root = tk.Tk()
root.withdraw()
TextInfo(self.root, window_title, textfield, label)  
# don't call root.mainloop() here, because this will lead to blocking.

Is there some kind of property or event that I need to set for the window to show up? If I call root.mainloop() the window will show up, but then my GUI is blocked again.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Don't use the tkMessageBox as it doesn't allow much configuration. Just create your own custom dialog that looks like one. This page talks a lot about creating custom Tkinter dialogs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...