我想 构建基于Python的简单GUI应用 ,这些应用可以分发给我的同事。在此过程中,我了解了Python 2.7版本的 PySimpleGUI27 。
以下是一个简单的代码,它生成带有菜单选项的窗口。然后,我使用 PyInstaller 创建了一个构建并对其进行了测试。但是,当我从“ dist”文件夹运行.exe版本时,它只会闪烁并消失。但是,当我从Python IDE中运行GUI脚本时,实际上可以看到GUI功能。
import PySimpleGUI27 as sg
sg.ChangeLookAndFeel('LightGreen')
sg.SetOptions(element_padding=(0, 0))
# ------ Menu Definition ------ #
menu_def = [['File', ['Open', 'Save', 'Exit']],
['Edit', ['Paste', ['Special', 'Normal', ], 'Undo'], ],
['Help', 'About...'], ]
# ------ GUI Definition ------ #
layout = [
[sg.Menu(menu_def, )],
[sg.Output(size=(60, 20))]
]
window = sg.Window("Windows-like program", default_element_size=(12, 1), auto_size_text=False, auto_size_buttons=False,
default_button_element_size=(12, 1)).Layout(layout)
# ------ Loop & Process button menu choices ------ #
while True:
event, values = window.Read()
if event == None or event == 'Exit':
break
print('Button = ', event)
# ------ Process menu choices ------ #
if event == 'About...':
sg.Popup('About this program', 'Version: 1.0', 'PyDist: Anaconda27')
elif event == 'Open':
filename = sg.PopupGetFile('file to open', no_window=True)
print(filename)
当我尝试运行exe文件时,它闪烁并消失。请查看出色的GIF here。任何建议表示赞赏。
使用Powershell从文件夹运行EXE。
PS C:\Users\user\AppData\Local\Continuum\anaconda3\envs\XCAL\Scripts\dist\PySimpleGUI_00> .\PySimpleGUI_00.exe
Traceback (most recent call last):
File "PySimpleGUI_00.py", line 1, in <module>
File "c:\users\user\appdata\local\temp\pip-install-qrr1qq\PyInstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
File "site-packages\PySimpleGUI27\__init__.py", line 2, in <module>
File "c:\users\user\appdata\local\temp\pip-install-qrr1qq\PyInstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
File "site-packages\PySimpleGUI27\PySimpleGUI27.py", line 13, in <module>
File "site-packages\future\standard_library\__init__.py", line 459, in install_aliases
ImportError: No module named UserList
[15328] Failed to execute script PySimpleGUI_00
答案 0 :(得分:1)
除了使用Python 3运行pyinstaller之外,我还建议使用Python 3版本的PySimpleGUI库。
使用这些选项运行pyinstaller
pyinstaller -wF demo.py
您的示例应该是使用纯PySimpleGUI导入的示例:
import PySimpleGUI as sg
sg.ChangeLookAndFeel('LightGreen')
sg.SetOptions(element_padding=(0, 0))
# ------ Menu Definition ------ #
menu_def = [['File', ['Open', 'Save', 'Exit']],
['Edit', ['Paste', ['Special', 'Normal', ], 'Undo'], ],
['Help', 'About...'], ]
# ------ GUI Definition ------ #
layout = [
[sg.Menu(menu_def, )],
[sg.Output(size=(60, 20))]
]
window = sg.Window("Windows-like program", default_element_size=(12, 1), auto_size_text=False, auto_size_buttons=False,
default_button_element_size=(12, 1)).Layout(layout)
# ------ Loop & Process button menu choices ------ #
while True:
event, values = window.Read()
if event == None or event == 'Exit':
break
print('Button = ', event)
# ------ Process menu choices ------ #
if event == 'About...':
sg.Popup('About this program', 'Version: 1.0', 'PyDist: Anaconda27')
elif event == 'Open':
filename = sg.PopupGetFile('file to open', no_window=True)
print(filename)