Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

python tkinter exe built with cx_Freeze for windows won't show GUI

PROBLEM SOLVED. the issue was with jaraco module, that i used for clipboard manipulation, i used pyperclip instead.

I made a python app with tkinter that works fine, but I wanted to make an exe from it so it's user friendly in windows. I used cx_Freeze library for that, that works fine too, but not always.

When creating the exe using cx_Freeze you can specify base parameter, if you give none that will open 2 windows, cmd window and a GUI window for your app. To get rid of the cmd window you can specify "Win32GUI" as base parameter for cx_Freeze.

This ignores the cmd window and just opens the GUI, it seems to be working, but not always. Sometimes opening the exe file will start the proccess but no GUI will show. Opening cmd and going to the directory of your exe and starting it from there will actually show the GUI and fix the problem untill you restart your pc (you can open the app without cmd with everything working untill restart)

It seems that as long as cmd window is in your ram, the GUI will show, otherwise it "doesn't know" and fails to show GUI.

The code can be found here: https://github.com/GrishaDev/ClipMagic

clip.py is the entire app

setup.py is the file used with cx_Freeze to get exe of the app, that's where you specify base parameter and such.

the piece of code where the problem most likely is (setup.py):

import sys
from cx_Freeze import setup, Executable
# import os
# os.environ['TCL_LIBRARY'] = "C:\Users\Grisha\AppData\Local\Programs\Python\Python35\tcl\tcl8.6"
# os.environ['TK_LIBRARY'] = "C:\Users\Grisha\AppData\Local\Programs\Python\Python35\tcl\tk8.6"
import os.path
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(name="clipmagic",
      version="1",
      description="Extended clipboard",
      options={'build_exe': {'includes': ["jaraco", "tkinter"], 'include_files':[
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
            'icon.ico',
         ]}},
      executables=[Executable("clip.py", base=base, icon='icon.ico')])

#"Win32GUI"

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looking at the README.md in your code repository, you are using the current version of cx_Freeze, which is 5.1.1. In this version, the included modules are in a subdirectory lib of the build directory. The manually added DLLs apparently need to be moved there as well. See this answer.

Try to make the following change to your setup.py script:

options={'build_exe': {'includes': ["jaraco", "tkinter"], 'include_files':[
    (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), os.path.join('lib', 'tk86t.dll')),
    (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), os.path.join('lib', 'tcl86t.dll')),
    'icon.ico',
    ]}},

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...