Python Forum
popup message box code runs in Windows, but not in Linux
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
popup message box code runs in Windows, but not in Linux
#1
This code is straight from a Bucky Roberts tutorial.  (https://www.youtube.com/watch?v=IB6VkXJVf0Y)

from tkinter import *

import tkinter.messagebox      ### Pull in ability to make message boxes

root = Tk()

tkinter.messagebox.showinfo('Window Title', 'Monkeys can live up to 300 years.')

root.mainloop()
I made sure to code it exactly like Bucky explained.  However, when I go to run it from PyCharm on my Kubuntu Linux PC, all I get is this plain window:
 [Image: Message_Boxproblem.png]
No popup message box comes up.  When I exit out, I get the following error code in the run box:
/usr/bin/python3.5 /home/thomas/Documents/python_work/GUIs/thenewboston/buckymessagebox.py
Traceback (most recent call last):
  File "/home/thomas/Documents/python_work/GUIs/thenewboston/buckymessagebox.py", line 7, in <module>
    tkinter.messagebox.showinfo('Window Title', 'Monkeys can live up to 300 years.')
  File "/usr/lib/python3.5/tkinter/messagebox.py", line 83, in showinfo
    return _show(title, message, INFO, OK, **options)
  File "/usr/lib/python3.5/tkinter/messagebox.py", line 72, in _show
    res = Message(**options).show()
  File "/usr/lib/python3.5/tkinter/commondialog.py", line 48, in show
    s = w.tk.call(self.command, *w._options(self.options))
_tkinter.TclError: can't invoke "grab" command: application has been destroyed

Process finished with exit code 1
On my Windows 7 PC, there's no problem.  I get the main window, plus the popup message box as shown:

[Image: Message_Box_Main_Win.png]
[Image: Message_Box_Win.png]

Why won't it work in my Kubuntu PC?  Is there some aspect of Python, tkinter, or a plugin for it that I have not installed and need to?

Edit: In case it's relevant, I have Python 3.5 on the Kubuntu PC and Python 3.6 on the Windows 7 one.
Reply
#2
Did you try running it from the command line instead of within an editor? The code looks simple, so I'd be surprised if there was an issue with Tk or python here.
Reply
#3
Turns out it was running the whole time.  The popup message box ran, but it was hidden back behind other windows.  For some reason it's not immediately being shown forward under Kubuntu like it is under Windows.

Edit: It would not run from the command line.  I get this from the command line:

thomas@thomas-HP-Compaq-6200-Pro-SFF-PC:~/Documents/python_work/GUIs/thenewboston$ Python buckymessagebox.py
No command 'Python' found, did you mean:
 Command 'jython' from package 'jython' (universe)
 Command 'python' from package 'python-minimal' (main)
 Command 'python' from package 'python3' (main)
 Command 'cython' from package 'cython' (universe)
Python: command not found
thomas@thomas-HP-Compaq-6200-Pro-SFF-PC:~/Documents/python_work/GUIs/thenewboston$ 
I was in the directory of the file "buckymessagebox.py".
Reply
#4
The error should help out. From the command line, python is all lowercase (for future reference, almost everything is all lowercase at the command line).
Reply
#5
Okay, thanks.  I'll take note of that.  It's still not running from the command line.  The latest attempt went as follows:

thomas@thomas-HP-Compaq-6200-Pro-SFF-PC:~/Documents/python_work/GUIs/thenewboston$ python buckymessagebox.py
Traceback (most recent call last):
  File "buckymessagebox.py", line 1, in <module>
    from tkinter import *
ImportError: No module named tkinter
thomas@thomas-HP-Compaq-6200-Pro-SFF-PC:~/Documents/python_work/GUIs/thenewboston$ 
I wonder if tkinter has to be on whatever the Linux equivalent of the path is.  I remember having to put things on the path back in my MS DOS days. 

As for the message box commands, I wonder if for any Linux version, I have to code in something that tells the message box to display forward.  Those message boxes are running, but the first one is always back behind.  I did a little more coding, adding more message boxes just for practice:

from tkinter import *

import tkinter.messagebox      ### Pull in ability to make message boxes

root = Tk()

tkinter.messagebox.showinfo('Window Title', 'Monkeys can live up to 300 years.')

answer = tkinter.messagebox.askquestion('Question 1', 'Do you like silly faces?')

if answer == 'yes':
    tkinter.messagebox.showinfo('Really', 'Here ya go 8===D~ ')
    # print(' 8===D~ ')
elif answer == 'no':
    tkinter.messagebox.showinfo('Really', 'You sure are boring')

newanswer = tkinter.messagebox.askquestion('Question 2', 'Are you a goofball?')

if newanswer == 'yes':
    tkinter.messagebox.showinfo('Really', 'That is awesome.  Enjoy')
elif newanswer == 'no':
    tkinter.messagebox.showinfo('Really', 'Yawn, why not live a little?')
else:
    tkinter.messagebox.showinfo('Really', 'Yeah, whatever.')

root.mainloop()
The first one, the one that reads "Monkeys can live up to 300 years" is always back behind.  I have to find it in the Kubuntu task bar and click on it to bring it forward.  Once that is done, all the others are then at the front where they should be.  I can't let that behavior be in the final version of an application.  If the user ran it, he might not realize that there was a message box back behind that was asking a question. 

Maybe there's some way to tell a message box "run in the foreground"?
Reply
#6
I think if you ran from the command line, it WOULD be in the foreground already :p
Do you know what version of python the editor is using? Maybe try running as "python3 myfile.py"
Reply
#7
Adding the 3 worked:
python3 buckymessagebox.py

However, it was exactly as it was when running from Pycharm.  The main menu came up and the message box was in the background until I found it in the task bar and clicked on it.
Reply
#8
(May-02-2017, 07:49 PM)Luke_Drillbrain Wrote: However, it was exactly as it was when running from Pycharm.  The main menu came up and the message box was in the background until I found it in the task bar and clicked on it.
You wouldn't really start a program and open up repetitive dialogs without something to initiate it, like a button. The program does what you ask it to do. It opens up root, then showinfo, then askquestion.

messagebox is for quick popups, if you want to customize thigns like window position, it would be easier to make your own dialogs as a Toplevel instead.

Try adding this code after you create root
root.attributes("-topmost", True)
Recommended Tutorials:
Reply
#9
An update:

I don't appear to have the same problem if I call the message box from a button or from a menu.  I put the message box code inside a def named doNothing in this code.  Either the "Insert Image" button or any of the menus can call it.  It runs fine.  The message boxes pop up in the foreground.  

### https://www.youtube.com/watch?v=PSm-tq5M-Dc  <== menus
### https://www.youtube.com/watch?v=D24Vx3_IM8U&t=23s   <=== Toolbar
from tkinter import *
import tkinter.messagebox      ### Pull in ability to make message boxes

def doNothing():
   # print("Okay, okay, I won't")
   answer = tkinter.messagebox.askquestion('Question 1', 'Do you like silly faces?')

   if answer == 'yes':
       tkinter.messagebox.showinfo('Really', 'Here ya go 8===D~ ')
       # print(' 8===D~ ')
   elif answer == 'no':
       tkinter.messagebox.showinfo('Really', 'You sure are boring')

def doSomething():
   filewin = Toplevel(root)
   button = Button(filewin, text="Do something button")
   button.pack()

root = Tk()

### ************* Main Menu ***********************************

menu = Menu(root)
root.config(menu=menu)

subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project ...", command=doNothing)
subMenu.add_command(label="New", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing)

editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=doNothing)

### ************   The Toolbar ***********************************

toolbar = Frame(root, bg="blue")

insertButt = Button(toolbar, text="Insert Image", command=doNothing)
insertButt.pack(side=LEFT, padx=2, pady=2)
printButt = Button(toolbar, text="Print", command=doSomething)
printButt.pack(side=LEFT, padx=10, pady=10)

toolbar.pack(side=TOP, fill=X)  ### fill=X makes it fill up all space in the X direction (horizontally)

### ************   The Statusbar ***********************************

status = Label(root, text="Preparing to do nothing...", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)  ### fill = X makes sure that it spans the entire bottom

root.mainloop()
So I think I'm good.  My worries were that I would create some real application and the user would choose something and the message box would be in the background unbeknownst to him, but that doesn't appear to be the case.
Reply
#10
I copied your original code, pasted it into a file which I saved as test1.py
I then ran it from the commandline :
python3 ./test1.py
and it worked fine on my Mint 18.1 system with Python 3.5.2
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Hover over highlighted text and open popup window DrakeSoft 2 1,512 Oct-29-2022, 04:30 PM
Last Post: DrakeSoft
  POPUP on widget Entry taratata2020 4 3,752 Mar-10-2020, 05:04 PM
Last Post: taratata2020
  [WxPython] How to show remove button at the right side of the hovering item of a combobox popup? indrajitmajumdar 0 2,513 Mar-28-2019, 11:24 AM
Last Post: indrajitmajumdar
  [Tkinter] [SOLVED] Create per button popup menu py2.7 AceScottie 5 6,176 May-31-2018, 12:39 AM
Last Post: AceScottie
  Code fails on Mac, works on Windows and Raspberry Pi eugenedakin 4 4,019 May-30-2018, 03:49 AM
Last Post: eugenedakin
  [PyQt] source code is not running in REDHAT 7 linux platform shridhara 0 2,117 May-23-2018, 07:58 AM
Last Post: shridhara
  Please advice Linux library - tray icon, popup windows, ICQ/Skype style etc ramendik 5 4,053 Dec-03-2017, 04:35 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020