Home > Software engineering >  Pyinstaller: Why some command actions don't work?
Pyinstaller: Why some command actions don't work?

Time:10-25

I designed a simple Python GUI with Tkinter. It includes a button which performs an action when it is pressed. Originally, it opens a new subprocess in order to run an external software. The code works fine when ran like a script, but when I converted into an application using Pyinstaller it doesn't work. No errors or warnings appear during the conversion (in Anaconda power shell) and during the .exe execution. So I decided to simplify it, showing a messagebox when the button is pressed. Once converted with Pyinstaller, again nothing happens. Here is the command I use for the conversion

pyinstaller --onefile -w ProgramName.py 

And this is the content of the script, for the GUI generation

import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *

# %% ROOT
root = tk.Tk()
root.geometry("710x630 410 100")
root.minsize(120, 1)
root.resizable(1,  1)
root.title("myGUI")
root.configure(background="#d9d9d9")
root.configure(highlightbackground="#d9d9d9")
root.configure(highlightcolor="black")   

# %% BUTTON OPEN FILE
Button_OpenFile = tk.Button(root)
Button_OpenFile.pack(fill=tk.BOTH, padx=10, pady=50)
Button_OpenFile.configure(activebackground="#ececec")
Button_OpenFile.configure(activeforeground="#000000")
Button_OpenFile.configure(background="#d9d9d9")
Button_OpenFile.configure(compound='left')
Button_OpenFile.configure(disabledforeground="#a3a3a3")
Button_OpenFile.configure(foreground="#000000")
Button_OpenFile.configure(highlightbackground="#d9d9d9")
Button_OpenFile.configure(highlightcolor="black")
Button_OpenFile.configure(text='''OPEN FILE''')
Button_OpenFile.configure(command=lambda: tk.messagebox.showinfo(
        title='Information',
        message='This is an information message.'))

root.mainloop()

CodePudding user response:

tkinter.messagebox is a module and needs to be imported separately and explicitly with the rest of your imports in order to use it.

To solve your problem all you need to do is add an import statement:

import sys
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.messagebox  # <-- added this
from tkinter.constants import *

# %% ROOT
root = tk.Tk()
root.geometry("710x630 410 100")
root.minsize(120, 1)
root.resizable(1,  1)
root.title("myGUI")
root.configure(background="#d9d9d9")
root.configure(highlightbackground="#d9d9d9")
root.configure(highlightcolor="black")   

# %% BUTTON OPEN FILE
Button_OpenFile = tk.Button(root)
Button_OpenFile.pack(fill=tk.BOTH, padx=10, pady=50)
Button_OpenFile.configure(activebackground="#ececec")
Button_OpenFile.configure(activeforeground="#000000")
Button_OpenFile.configure(background="#d9d9d9")
Button_OpenFile.configure(compound='left')
Button_OpenFile.configure(disabledforeground="#a3a3a3")
Button_OpenFile.configure(foreground="#000000")
Button_OpenFile.configure(highlightbackground="#d9d9d9")
Button_OpenFile.configure(highlightcolor="black")
Button_OpenFile.configure(text='''OPEN FILE''')
Button_OpenFile.configure(command=lambda: tk.messagebox.showinfo(
        title='Information',
        message='This is an information message.'))

root.mainloop()

Additionally it's always good practice to compile your application without the -w flag the first time so that you can see the error messages that pop to make debugging much easier. Then once your app runs smoothly, add the -w back to your pyinstaller command.

  • Related