简体   繁体   English

为什么 Kivy App.root 实例在 Pyinstaller 构建后无法识别 root 成员方法?

[英]Why Kivy App.root instance does not recognize root member method after built by Pyinstaller?

I am making a small Kivy app, it basically works fine in development environment.我正在制作一个小型 Kivy 应用程序,它在开发环境中基本上可以正常工作。 However, it crashed when I launch the built exe file.但是,当我启动构建的 exe 文件时它崩溃了。

build command used:使用的构建命令:
pyinstaller --onefile main.py

Basically, the method store_section() is defined in RootWidget and should be called from on_start() method of App instance.基本上,方法store_section()是在 RootWidget 中定义的,应该从 App 实例的on_start()方法中调用。 And eveything just works well by invoking directly to main.py ( python main.py ).通过直接调用 main.py ( python main.py ),一切都可以很好地工作。 Even Pyinstaller built successfully, I got the following error message when launching the corresponding exe file.即使 Pyinstaller 构建成功,我在启动相应的 exe 文件时也收到以下错误消息。 The error said:错误说:
" AttributeError: 'Widget' object has no attribute 'store_sections' ". AttributeError: 'Widget' object 没有属性 'store_sections' ”。

[INFO   ] [Logger      ] Record log in C:\Users\hoang\.kivy\logs\kivy_21-06-03_46.txt
[INFO   ] [Kivy        ] v2.0.0
[INFO   ] [Kivy        ] Installed at "C:\Users\hoang\AppData\Local\Temp\_MEI93602\kivy\__init__.pyc"
[INFO   ] [Python      ] v3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "D:\ielts\dist\main.exe"
[INFO   ] [Factory     ] 186 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.6.0 - Build 26.20.100.7927'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel(R) UHD Graphics 620'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60 - Build 26.20.100.7927'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
 Traceback (most recent call last):
   File "main.py", line 152, in <module>
     IeltsApp().run()
   File "kivy\app.py", line 949, in run
   File "kivy\app.py", line 944, in _run_prepare
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "main.py", line 148, in on_start
     self.root.store_sections()
 AttributeError: 'Widget' object has no attribute 'store_sections'
[2264] Failed to execute script main

Below is main.py and kv file.下面是 main.py 和 kv 文件。

main.py主文件

class Section(GridLayout):
    sectionNumber = NumericProperty(None)
    questions = ListProperty([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

class RootWidget(FloatLayout):
    sections = []
    
    # Other methods and properties are removed for short

    def store_sections(self):
        self.sections.append(self.sectionOne)
        self.sections.append(self.sectionTwo)
        self.sections.append(self.sectionThree)
        self.sections.append(self.sectionFour)


class IeltsApp(App):

    def on_start(self):
        self.root.store_sections()


if __name__ == '__main__':
    IeltsApp().run()

ielts.kv file ielts.kv 文件

RootWidget:
    sectionOne: sectionOne
    sectionTwo: sectionTwo
    sectionThree: sectionThree
    sectionFour: sectionFour

    GridLayout:
        size_hint: (1, .95)
        pos_hint: {'x': 0, 'y': (root.height - self.height)/root.height}
        cols: 2
        ScrollView:
            GridLayout:
                rows: 2
                size_hint_y: None
                height: self.minimum_height
                Section:
                    id: sectionOne
                    sectionNumber: 1
                    questions: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
                Section:
                    id: sectionTwo
                    sectionNumber: 2
                    questions: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
        ScrollView:
            GridLayout:
                rows: 2
                size_hint_y: None
                height: self.minimum_height
                Section:
                    id: sectionThree
                    sectionNumber: 3
                    questions: [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
                Section:
                    id: sectionFour
                    sectionNumber: 4
                    questions: [31, 32, 33, 34, 35, 36, 37, 38, 39, 40]

Could someone please help me tackle this error?有人可以帮我解决这个错误吗? Thank you so much.太感谢了。

I don't think the kv file will be automatically included in the .exe .我认为kv文件不会自动包含在.exe中。 Try running:尝试运行:

pyinstaller --add-data 'ielts.kv';'.' --onefile main.py

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

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