14210

Tkinter tkMessageBox disables Tkinter key bindings

Question:

Here's a very simple example:

from Tkinter import * import tkMessageBox def quit(event): exit() root = Tk() root.bind("<Escape>", quit) #tkMessageBox.showinfo("title", "message") root.mainloop()

If I run the code <strong>exactly</strong> as it is, the program will terminate when <kbd>Esc</kbd> is hit. Now, if I un-comment the tkMessageBox line, the binding is "lost" after closing the message box, i.e. pressing <kbd>Esc</kbd> won't do anything anymore. This is happening in Python 2.7. Can you please verify if this is happening also to you? And let me know about your Python version.

<hr />

Here is a way to "by-pass" the problem. It's a different approach, but it might help:

from Tkinter import * import tkMessageBox def msg_test(): tkMessageBox.showinfo("title", "message") def quit(event): exit() root = Tk() root.bind("<Escape>", quit) btn = Button(root, text="Check", command=msg_test); btn.pack() root.mainloop()

Using tkMessageBox via a button click, doesn't affect key binding, i.e. pressing <kbd>Esc</kbd> continues to work.

Answer1:

If I understand the problem, you get the bad behavior if you call tkMessageBox.showInfo() <em>before</em> calling mainloop. If that is so, I think this is a known bug in tkinter on windows.

The solution is simple: don't do that. If you need a dialog to show at the very start of your program, use after to schedule it to appear after mainloop has started, or call update before displaying the dialog.

For example:

root = Tk() root.after_idle(msg_test) root.mainloop() <hr />

The original bug was reported quite some time ago, and the tk bug database has moved once or twice so I'm having a hard time finding a link to the original issue. Here's one issue from 2000/2001 that mentions it: <a href="https://core.tcl.tk/tk/tktview?name=220431ffff" rel="nofollow">https://core.tcl.tk/tk/tktview?name=220431ffff</a> (see the comments at the very bottom of the bug report).

The report claims it was fixed, but maybe it has shown up again, or maybe your version of tkinter is old enough to still have the bug.

Recommend

  • How to give border to text in PHP GD library
  • multiple versions of Mongo
  • Firebase tracking/limiting of user request volume
  • Normal Query on Cassandra using DataStax Enterprise works, but not solr_query
  • Condition on a timestamp column to select data for a year
  • Weights and Biases not updating in tensorflow
  • Best practice to include console app in NuGet
  • Decompress string in java from compressed string in C#
  • How to change the default browser in visual studio code latest released?
  • Select multiple fields with single group by in django
  • Converting a wstring to jstring on Linux
  • How to customize marker colors and shapes in scatter plot?
  • Reading large text file very slow
  • How to add tag during runtime in C#
  • How to change WebBrowser fullscreen video mode?
  • Existing data serialized as hash produces error when upgrading to Rails 5
  • JSF validateLength question
  • Coin change recursive approach
  • Sprite animation wobbly / jumping in IE11
  • DocuSign API Replace template document but keep fields
  • A library that can fill Java object with data declared in Excel file
  • Gradle refresh removes source folders in build path
  • Eric5: The OK button of 'new project' dialog is disable
  • jQuery - Prevent activation on first click
  • Making Django.contrib.auth store plain-text password
  • Failed to resolve: firebase-auth-15.0.0 [closed]
  • Can't hide status bar in AVPlayerViewController's portrait mode
  • How to put an object in the air?
  • Sample deviceQuery cuda program
  • Modifying native query cannot have named parameter bindings?
  • Android studio import problems. (Apktool)
  • How to control xtics in gnuplot
  • How to display converted time zones in a 'generic week' (Sunday thru Saturday)?
  • 2d barcode reader Java ME sdk
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • Compiling multiple source files in Rcpp
  • trigger ontouch event programmatically
  • Django, uWSGI & nginx: Process dies for “no reason”
  • Typeahead.js does give me suggestions but doesn't select them
  • Neo4j…how to get a visual representation of my data?