Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

non blocking info dialog in Tkinter

Tags:

python

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.

like image 202
memyself Avatar asked Jul 10 '12 17:07

memyself


People also ask

How do I open a dialog box in Tkinter?

import tkinter as tk from tkinter import simpledialog application_window = tk.Tk() answer = simpledialog. askstring("Input", "What is your first name?", parent=application_window) if answer is not None: print("Your first name is ", answer) else: print("You don't have a first name? ") answer = simpledialog.

What is Tk () in Tkinter Python?

The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems. (Tk itself is not part of Python; it is maintained at ActiveState.)

How do I display a dialog box in Python?

The tkMessageBox module is used to display message boxes in your applications. This module provides a number of functions that you can use to display an appropriate message. Some of these functions are showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, and askretryignore.


1 Answers

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.

like image 196
Mike Driscoll Avatar answered Sep 30 '22 17:09

Mike Driscoll