Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter messagebox without window?

Tags:

python

tkinter

I want to show an info window in my python script running on ubuntu. I'm using the following code:

import tkMessageBox
tkMessageBox.showinfo("Say Hello", "Hello World")

This works, but there's an empty window displayed, with the message box on top. How can I get rid of the window and just centre the message box on the screen (window manager is gnome 2)?

This is just to display some info from a command line script (a password which is why I don't want to just echo it to the console).

like image 285
user1491250 Avatar asked Jun 24 '13 16:06

user1491250


People also ask

What are the different types of Messagebox available in message widget of Tkinter module?

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.

How do you show error box in Python?

Build A Paint Program With TKinter and Python If you need to display the error messagebox in your application, you can use showerror("Title", "Error Message") method. This method can be invoked with the messagebox itself.

How do you use Askyesno Tkinter?

Summary. Use the Tkinter askyesno() function to show a dialog that asks for user confirmation. The askyesno() function returns True if you click the yes button, otherwise, it returns False . The askquestion() function returns a string with a value of 'yes' or 'no' instead.


3 Answers

Tkinter must have a root window. If you don't create one, one will be created for you. If you don't want this root window, create it and then hide it:

import Tkinter as tk
root = tk.Tk()
root.withdraw()
tkMessageBox.showinfo("Say Hello", "Hello World")

Your other choice is to not use tkMessageBox, but instead put your message in the root window. The advantage of this approach is you can make the window look exactly like you want it to look.

import Tkinter as tk
root = tk.Tk()
root.title("Say Hello")
label = tk.Label(root, text="Hello World")
label.pack(side="top", fill="both", expand=True, padx=20, pady=20)
button = tk.Button(root, text="OK", command=lambda: root.destroy())
button.pack(side="bottom", fill="none", expand=True)
root.mainloop()

(personally I would choose a more object-oriented approach, but I'm trying to keep the code small for this example)

like image 179
Bryan Oakley Avatar answered Oct 28 '22 09:10

Bryan Oakley


To avoid a "flash" as the root window is created, use this slight variation on the accepted answer:

import Tkinter as tk
root = tk.Tk()
root.overrideredirect(1)
root.withdraw()
tkMessageBox.showinfo("Say Hello", "Hello World")
like image 23
BuvinJ Avatar answered Oct 28 '22 09:10

BuvinJ


For Python 3:

import tkinter, tkinter.messagebox

def messagebox(title, text):
    root = tkinter.Tk()
    root.withdraw()
    tkinter.messagebox.showinfo(title, text)
    root.destroy()

With native Windows support when pywin32 is installed:

try:
    from win32ui import MessageBox
except ImportError:
    import tkinter, tkinter.messagebox
    def MessageBox(text, title):
        root = tkinter.Tk()
        root.withdraw()
        tkinter.messagebox.showinfo(title, text)
        root.destroy()
like image 5
user Avatar answered Oct 28 '22 11:10

user