【发布时间】:2021-06-03 18:18:27
【问题描述】:
我正在制作一个小型 Kivy 应用程序,它在开发环境中基本上可以正常工作。 但是,当我启动构建的 exe 文件时它崩溃了。
使用的构建命令:pyinstaller --onefile main.py
基本上,store_section()方法是在RootWidget中定义的,应该从App实例的on_start()方法中调用。通过直接调用 main.py (python main.py),一切都可以很好地工作。
即使 Pyinstaller 构建成功,我在启动相应的 exe 文件时也收到以下错误消息。错误说:
"AttributeError: 'Widget' 对象没有属性 '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文件。
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 文件
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]
有人可以帮我解决这个错误吗? 非常感谢。
【问题讨论】:
-
显然
self.root不是您所期望的,那么它是什么以及它来自哪里?理解这一点可能有助于理解问题。 -
Kv代码存储在一个名为ielts.kv的文件中,因此kivy框架在启动IeltsApp().run()后会自动从那里加载kv结构,因此self.root就是RootWidget的实例。很抱歉没有在我的问题中澄清清楚..
-
这是应该的,而不是(根据错误)它是什么。你应该调查一下。例如,如果您将
self.root.store_sections()注释掉会发生什么,您是否看到了您期望的小部件? -
我会在注释掉该行并更新您之后尝试重建
-
你是对的,@inclement。注释掉
self.root.store_sections()后,app只显示一个黑色窗口,self.root实际上是kivy.uix.widget.Widget,这就解释了为什么它没有属性store_sections
标签: python python-3.x kivy