42

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

user1491250
  • 1,831
  • 4
  • 18
  • 21

5 Answers5

61

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)

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Is there any way I can make the text selectable so I can copy it? – user1491250 Jun 24 '13 at 17:01
  • @user1491250: yes. Use a text widget instead of a label. You can tweak the borderwidth and colors to make it look like a label. – Bryan Oakley Jun 24 '13 at 17:23
  • With the first option...Is there a way to finish the app when pressing the "OK" button in the dialog? Right now, the dialog doesn't close when I do (maybe I should do a root.destroy(), but I don't know how to do it just after pressing OK) – madtyn Jul 11 '16 at 09:34
12

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")
BuvinJ
  • 10,221
  • 5
  • 83
  • 96
5

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()
user
  • 23,260
  • 9
  • 113
  • 101
0

Import messagebox individually. For example:

from tkinter import *
import tkinter.messagebox

or

from tkinter import messagebox
Jason
  • 945
  • 1
  • 9
  • 17
Meku
  • 17
  • 1
0

This works for python 3

from tkinter import *
from tkinter import messagebox

root = Tk()
root.withdraw()
messagebox.showinfo("Window Title", "Your Message")
Rahul Sarma
  • 763
  • 2
  • 8
  • 17