Python Forum
[Tkinter] How to make pyinstaller import the ttk theme?
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to make pyinstaller import the ttk theme?
#1
I have a project done and it is in the following:
import tkinter as tk
from tkinter import ttk
import tkinter.messagebox as tmb
from ttkthemes import ThemedStyle

class UI(ttk.Frame):
    def __init__(self, parent=None):
        super(UI,  self).__init__(parent)
        self.parent = parent
        style = ThemedStyle(parent)
        style.set_theme("arc")
        self.parent.geometry("480x520")
        self.init_ui()

    def init_ui(self):
        self.parent.title("New window")
        self.entrada = ttk.Entry(self, text="insert")
        self.entrada.grid(row=2, column=2) 
        boton = ttk.Button(self, text="puls", command=self.ver)
        boton.grid(row=2, column=3)
        self.pack()

    def ver(self):
        try:
            res = int(self.entrada.get())
            print(res)
        except ValueError:
            tmb.showwarning(title = "error", message = " error")

if __name__== '__main__':
    root = tk.Tk() 
    sug = tk.Label(root, text="example missing")
    sug.pack()
    app = UI(parent=root)
    app.mainloop()
When I use pyinstaller to create an exe, it releases the error that the exported exe program can not be executed: below I leave it as I use it, it should be noted that I have used a thema for the program and I doubt that it is imported.
pyinstaller --windowed ui.py -i icono.ico --onefile
obtain
---------------
error

Fatal Error! Failed to execute script ui
[Image: FyqBt.png]
[Image: EIj2t.png]
[Image: bx5Ci.png]
Reply
#2
I don't use windows and so haven't tried to create an exe file so I cant be sure. However the error message says where it is looking for ttk themes and says it cant find it there. Check if it is there first. If it isn't then I would think the easiest way to solve the problem is to copy it to that place and see what happens
Reply
#3
Cross-posted at SO
https://stackoverflow.com/questions/4738...-ttk-theme
Reply
#4
(Nov-20-2017, 11:04 AM)buran Wrote: Cross-posted at SO
https://stackoverflow.com/questions/4738...-ttk-theme
Please I still don't have solution
Reply
#5
The docs actually say to run it without --window, with --debug, and run it in a terminal/command prompt. Dont use --onefile for debugging instead use --onedir

Im not familiar with pyinstaller, but you dont seem to have indicated you have gone through the steps of debugging.
https://github.com/pyinstaller/pyinstall...s-Go-Wrong

Have you used py-archive_viewer?
https://pythonhosted.org/PyInstaller/adv...ive-viewer
Recommended Tutorials:
Reply
#6
Did a test got it to work,i used pipenv to make virtual environment.
E:\1py_div\ttk
λ pip install -U pipenv
# Now pipenv
λ pipenv install pyinstaller
λ pipenv install pypiwin32
λ pipenv install ttkthemes
pipenv shell to activate folder.
Build with pyinstaller to add package/module --hidden-import ttkthemes:
# See that right pyinstaller is used.
E:\1py_div\ttk
λ which pyinstaller
/c/Users/Tom/.virtualenvs/ttk-Ki9goeNt/Scripts/pyinstaller

E:\1py_div\ttk
λ pyinstaller tk_test.py --hidden-import ttkthemes
Output:
718 INFO: Analyzing base_library.zip ... 5089 INFO: Analyzing hidden import 'ttkthemes' 5327 INFO: running Analysis out00-Analysis.toc
It find ttkthemes,but also have to copy ttkthemes folder in site-packages virtual environment to dist folder.
This can be because the way they have build ttkthemes with own data folders for theme.
Reply
#7
A little more testing.
All packet to one exe file.
This is spec file.
# -*- mode: python -*-
block_cipher = None

a = Analysis(['tk_test.py'],
             pathex=['E:\\1py_div\\ttk'],
             binaries=[],
             hiddenimports=['ttkthemes'],
             hookspath=[],
             datas = [('E:/1py_div/ttk/ttkthemes', 'ttkthemes')],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
a.datas += [('tv.ico', 'E:/1py_div/ttk/tv.ico', 'DATA')] 

pyz = PYZ(a.pure, a.zipped_data,
          cipher=block_cipher)           

# build one file --onefile         
exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    name='tk_test',
    debug=False,
    strip=False,
    upx=True,
    console=False,
    icon='E:/1py_div/ttk/tv.ico'   
) 
I you don't know so every time run from command line a spec file is auto-generated.
Then can edit this file,to avoid long command line commands.

This add package/module:
hiddenimports=['ttkthemes'],
This add folder:
datas = [('E:/1py_div/ttk/ttkthemes', 'ttkthemes')]
Run spec file:
E:\1py_div\ttk
λ pyinstaller --clean tk_test.spec
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Theme does not change globally TheTechRobo 2 3,321 Apr-21-2020, 04:00 PM
Last Post: deanhystad
  How can I get Colors of current GTK theme? ondoho 2 6,343 May-26-2017, 03:32 PM
Last Post: ondoho

Forum Jump:

User Panel Messages

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