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

前提是你 提交于 2019-12-25 18:41:53

问题


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!


回答1:


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',
    ]}},


来源:https://stackoverflow.com/questions/52319559/python-tkinter-exe-built-with-cx-freeze-for-windows-wont-show-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!