Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkinter showerror creating blank tk window

I have a program that needs to display graphical error messages to users. It is a tkinter GUI, so I am using tkinter.messagebox.showerror

When I call showerror, it shows the error, but also creates a blank "tk" window, the kind created when an instance of the Tk class is called, like root = Tk().

from tkinter.messagebox import showerror
showerror(title = "Error", message = "Something bad happened")

Produces

Results Of Above Code

How can I make this blank window not appear?

like image 404
Ecko Avatar asked Jun 16 '15 19:06

Ecko


People also ask

How do I remove a blank window in Tkinter?

As explained in this answer, Tkinter requires a root window before we create any more widgets/dialogs. If there is no root window, tkinter creates one. So, to make the blank window disappear, first we need to create a root window ourselves, hide it and destroy it once your dialog action is complete. Sample code below

How to create a root window in Tkinter GUI?

The tkinter GUI module provides a class Tk. We use the contructor of Tk class to craete an instance which is the root window of the program. Imports tkinter module, use tk as an alias. Because tk is short and easy to use in code. Header of main () function. Indented body of main () function . Create main window or Root window of the application.

How to create a full screen window in Tkinter?

- GeeksforGeeks How to Create Full Screen Window in Tkinter? There are two ways to create a full screen window in tkinter using standard python library for creating GUI applications. We will set the parameter ‘-fullscreen’ of attributes () to True for setting size of our window to fullscreen and to False otherwise.

How to create a borderless window using Tkinter?

Let us suppose that we want to create a borderless window using tkinter. To create a borderless window, we can use the overrideredirect method which basically disables the window and removes the window element such as the closing button, title, minimization element and buttons, etc.


2 Answers

from Tkinter import *
from tkMessageBox import showerror
Tk().withdraw()
showerror(title = "Error", message = "Something bad happened")

Calling Tk().withdraw() before showing the error message will hide the root window.

Note: from tkinter import * for Python 3.x

like image 102
maccartm Avatar answered Oct 09 '22 04:10

maccartm


As explained in this answer, Tkinter requires a root window before we create any more widgets/dialogs. If there is no root window, tkinter creates one. So, to make the blank window disappear, first we need to create a root window ourselves, hide it and destroy it once your dialog action is complete. Sample code below

from tkinter import Tk
from tkinter.messagebox import showerror

root = Tk()
root.withdraw()
showerror(title = "Error", message = "Something bad happened")
root.destroy()

Note: This is applicable when you just have to display a dialog and no other window exists.

like image 24
Nagabhushan S N Avatar answered Oct 09 '22 03:10

Nagabhushan S N