我想基于 Python 构建简单的 GUI 应用,可以分发给我的同事。在这个过程中,我了解了 PySimpleGUI27 适用于 Python 2.7 版本。
以下是生成带有菜单选项的窗口的简单代码。然后我使用了 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
请您参考如下方法:
除了使用 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)
