Python Forum
popup message box code runs in Windows, but not in Linux - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: popup message box code runs in Windows, but not in Linux (/thread-3169.html)

Pages: 1 2


popup message box code runs in Windows, but not in Linux - Luke_Drillbrain - May-02-2017

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.


RE: popup message box code runs in Windows, but not in Linux - nilamo - May-02-2017

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.


RE: popup message box code runs in Windows, but not in Linux - Luke_Drillbrain - May-02-2017

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


RE: popup message box code runs in Windows, but not in Linux - nilamo - May-02-2017

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


RE: popup message box code runs in Windows, but not in Linux - Luke_Drillbrain - May-02-2017

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"?


RE: popup message box code runs in Windows, but not in Linux - nilamo - May-02-2017

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"


RE: popup message box code runs in Windows, but not in Linux - Luke_Drillbrain - May-02-2017

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.


RE: popup message box code runs in Windows, but not in Linux - metulburr - May-02-2017

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



RE: popup message box code runs in Windows, but not in Linux - Luke_Drillbrain - May-02-2017

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.


RE: popup message box code runs in Windows, but not in Linux - Barrowman - May-07-2017

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