简体   繁体   English

pyinstaller 和 pygubu 的日历框架导致问题

[英]Pyinstaller and pygubu's calendar frame cause a problem

I made a pygubu python application, and decided I wanted to freeze it, after troubleshooting some simple problems, I ran into something I couldn't fix:我做了一个 pygubu python 应用程序,并决定冻结它,在解决了一些简单的问题后,我遇到了一些我无法解决的问题:

Traceback (most recent call last):
  File "main.py", line 138, in <module>
  File "pygubu\__init__.py", line 30, in __init__
  File "main.py", line 38, in _create_ui
  File "pygubu\builder\__init__.py", line 166, in get_object
  File "pygubu\builder\__init__.py", line 215, in _realize
  File "pygubu\builder\__init__.py", line 215, in _realize
  File "pygubu\builder\__init__.py", line 225, in _realize
Exception: Class "pygubu.builder.widgets.calendarframe" not mapped
[13108] Failed to execute script 'main' due to unhandled exception!

I even imported a bunch of pyugubu modules to try to fix this, and it still didn't work:我什至导入了一堆 pyugubu 模块来尝试解决这个问题,但它仍然不起作用:

from pygubu import builder
from pygubu.builder import ttkstdwidgets
from pygubu.builder import widgets
from pygubu.widgets import calendarframe
from pygubu import widgets

This probably doesn't matter, but I froze it with auto-py-to-exe.这可能无关紧要,但我用 auto-py-to-exe 冻结了它。 I've tried making it a console application, folder application, and both at the same time, but it still didn't work.我曾尝试将其同时用作控制台应用程序、文件夹应用程序以及两者,但它仍然无法正常工作。

Thank you in advance.先感谢您。

1. Create test-project 1.创建测试项目

Create test.py (adapted from here ) (i included all the dependencies you mentioned and listed their attributes and methods to make sure the module is included):创建test.py (改编自此处)(我包含了您提到的所有依赖项并列出了它们的属性和方法以确保包含模块):

import tkinter as tk
import pygubu
from pygubu import builder
from pygubu.builder import ttkstdwidgets
from pygubu.builder import widgets
from pygubu.widgets import calendarframe
from pygubu import widgets

class HelloWorldApp:
    
    def __init__(self):

        #1: Create a builder
        self.builder = builder = pygubu.Builder()

        #2: Load an ui file
        builder.add_from_file('test.ui')

        #3: Create the mainwindow
        self.mainwindow = builder.get_object('mainwindow')
        
    def run(self):
        self.mainwindow.mainloop()


if __name__ == '__main__':
    # just some calls to make sure the imports are loaded
    print(dir(pygubu))
    print(dir(builder))
    print(dir(ttkstdwidgets))
    print(dir(widgets))
    print(dir(calendarframe))
    print(dir(widgets))
    
    app = HelloWorldApp()
    app.run()

Create test.ui :创建test.ui

<?xml version='1.0' encoding='utf-8'?>
<interface>
  <object class="tk.Toplevel" id="mainwindow">
    <property name="height">200</property>
    <property name="resizable">both</property>
    <property name="title" translatable="yes">Hello World App</property>
    <property name="width">200</property>
    <child>
      <object class="ttk.Frame" id="mainframe">
        <property name="height">200</property>
        <property name="padding">20</property>
        <property name="width">200</property>
        <layout>
          <property name="column">0</property>
          <property name="propagate">True</property>
          <property name="row">0</property>
          <property name="sticky">nsew</property>
          <rows>
            <row id="0">
              <property name="weight">1</property>
            </row>
          </rows>
          <columns>
            <column id="0">
              <property name="weight">1</property>
            </column>
          </columns>
        </layout>
        <child>
          <object class="ttk.Label" id="label1">
            <property name="anchor">center</property>
            <property name="font">Helvetica 26</property>
            <property name="foreground">#0000b8</property>
            <property name="text" translatable="yes">Hello World !</property>
            <layout>
              <property name="column">0</property>
              <property name="propagate">True</property>
              <property name="row">0</property>
            </layout>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

2. Install pyinstaller and pygubu 2.安装pyinstaller和pygubu

pip install pyinstaller pygubu

3. Copy pygubu module to local project 3.将pygubu模块复制到本地项目

  1. Navigate to site-packages folder of current interpreter (execute in shell: python -c "import site; print(site.getsitepackages()[-1])" , navigate to returned path)导航到当前解释器的 site-packages 文件夹(在 shell 中执行: python -c "import site; print(site.getsitepackages()[-1])" ,导航到返回的路径)
  2. Copy entire pygubu folder to location with test.py script使用test.py脚本将整个pygubu文件夹复制到位置

One-liner from within the folder containing test.py :包含test.py的文件夹中的单行:

python -c "import shutil, site, os; shutil.copytree(site.getsitepackages()[-1]+os.sep+'pygubu', os.getcwd()+os.sep+'pygubu')"

4. Run pyinstaller, test package 4.运行pyinstaller,测试package

The following statement adds the .ui file as well as the entire pygubu package directly.以下语句直接添加.ui文件以及整个 pygubu package。 Afterwards, run the test.exe from within the dist/test folder.然后,从dist/test文件夹中运行test.exe

pyinstaller --clean --add-data=test.ui;. --add-data pygubu;pygubu test.py

I tried running pyinstaller with --onefile which did not work, --hidden-import , --collect-data , --collect-submodules and --collect-all also failed.我尝试使用 --onefile 运行 pyinstaller, --onefile它不起作用, --hidden-import--collect-data--collect-submodules--collect-all也失败了。 This is quite an ugly solution, however.然而,这是一个非常丑陋的解决方案。


Edit编辑

After discovering what an awesome module auto-py-to-exe is (thank you for mentioning it), i discovered that you can use --collect-all in combination with --onefile without the need of copying the pygubu library by hand.在发现一个很棒的模块 auto-py-to-exe 是什么之后(感谢您提及它),我发现您可以将--collect-all--onefile结合使用,而无需手动复制pygubu库。 However, i could not manage to include the.ui file, so that i had to copy manually:但是,我无法设法包含 .ui 文件,因此我不得不手动复制:

pyinstaller --noconfirm --onefile --windowed --collect-all "pygubu"  "test.py" && python -c "import shutil, os; shutil.copyfile('test.ui', 'dist'+os.sep+'test.ui')"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM