簡體   English   中英

為什么 Kivy App.root 實例在 Pyinstaller 構建后無法識別 root 成員方法?

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

我正在制作一個小型 Kivy 應用程序,它在開發環境中基本上可以正常工作。 但是,當我啟動構建的 exe 文件時它崩潰了。

使用的構建命令:
pyinstaller --onefile main.py

基本上,方法store_section()是在 RootWidget 中定義的,應該從 App 實例的on_start()方法中調用。 通過直接調用 main.py ( python main.py ),一切都可以很好地工作。 即使 Pyinstaller 構建成功,我在啟動相應的 exe 文件時也收到以下錯誤消息。 錯誤說:
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

下面是 main.py 和 kv 文件。

主文件

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 文件

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]

有人可以幫我解決這個錯誤嗎? 太感謝了。

我認為kv文件不會自動包含在.exe中。 嘗試運行:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM