2

我想构建基于 Python 的简单 GUI 应用程序,可以分发给我的同事。在这个过程中,我了解了PySimpleGUI27 for 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)

运行脚本时生成的 GUI 1

当我尝试运行 exe 文件时,它会闪烁并消失。请在这里查看很棒的 GIF 。任何建议表示赞赏。

使用 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
4

1 回答 1

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)
于 2018-11-02T17:39:22.990 回答