newpy
newpy

Reputation: 114

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. However, it crashed when I launch the built exe file.

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. And eveything just works well by invoking directly to main.py (python main.py). Even Pyinstaller built successfully, I got the following error message when launching the corresponding exe file. The error said:
"AttributeError: 'Widget' object has no attribute '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

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

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.

Upvotes: 0

Views: 141

Answers (1)

John Anderson
John Anderson

Reputation: 39052

I don't think the kv file will be automatically included in the .exe. Try running:

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

Upvotes: 1

Related Questions