Kivy多窗口切换应用出现AttributeError: release问题求助
问题描述
开发一个包含三个按钮的Kivy应用,实现窗口切换功能,涵盖调查、超链接、清单三类界面。运行时出现AttributeError: release错误,无法定位问题,以下是代码及报错信息。
Python代码(Test Integration.py)
from kivy.app import App from kivy.lang import Builder from kivy.uix.screenmanager import ScreenManager, Screen class FirstWindow(Screen): pass class SecondWindow(Screen): pass class ThirdWindow(Screen): pass class WindowManager(ScreenManager): pass kv = Builder.load_file("Test Integration.kv") class MyMainApp(App): def build(self): return kv if __name__ == "__main__": MyMainApp().run()
KV文件(Test Integration.kv)
WindowManager: FirstWindow: SecondWindow: ThirdWindow: <FirstWindow>: name: "Main Menu" BoxLayout: orientation: "vertical" size: root.width, root.height Label: text: "Safety Checklist" font_size: 24 on_release: app.root.current = "Checklist" root.manager.transition = "left" name: "Test Button." Button: text: "Please complete our survey!" on_release: import webbrowser webbrowser.open('https://forms.gle/k6cfYEU1snzpykxY9') name: "Checklist" BoxLayout: orientation: "vertical" size: root.width, root.height Label: text: "Checklist: Do you acquired all the necessary safety equipment?" font_size: 24 GridLayout: cols:2 Label: text: "Do you have your safety gloves with you?" font_size:16 CheckBox: on_active: root.checkbox_click(self, self.active, "Do you have your safety gloves with you?") Label: text: "Do you have your safety boots with you?" font_size:16 CheckBox: on_active: root.checkbox_click(self, self.active, "Do you have your safety boots with you?") Label: text: "Do you have your safety goggles with you?" font_size:16 CheckBox: on_active: root.checkbox_click(self, self.active, "Do you have your safety goggles with you?") Label: text: "Do you have your helmet with you?" font_size:16 CheckBox: on_active: root.checkbox_click(self, self.active, "Do you have your helmet with you?") Label: id: output_label text: "You are recommended to acquire or purchase the missing safety equipment." Button: text: "Return to main menu." on_release: app.root.current = "Main Menu" root.manager.transition = "right"
报错信息
Traceback (most recent call last): File "C:\Users\nicho\PycharmProjects\Temasek Polytechnic MP (Software App)\venv\lib\site-packages\kivy\lang\builder.py", line 729, in _apply_rule raise AttributeError(key) AttributeError: release During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\nicho\PycharmProjects\Temasek Polytechnic MP (Software App)\Test Integration.py", line 17, in <module> kv = Builder.load_file("Test Integration.kv") File "C:\Users\nicho\PycharmProjects\Temasek Polytechnic MP (Software App)\venv\lib\site-packages\kivy\lang\builder.py", line 305, in load_file return self.load_string(data, **kwargs) File "C:\Users\nicho\PycharmProjects\Temasek Polytechnic MP (Software App)\venv\lib\site-packages\kivy\lang\builder.py", line 407, in load_string self._apply_rule( File "C:\Users\nicho\PycharmProjects\Temasek Polytechnic MP (Software App)\venv\lib\site-packages\kivy\lang\builder.py", line 660, in _apply_rule child.apply_class_lang_rules( File "C:\Users\nicho\PycharmProjects\Temasek Polytechnic MP (Software App)\venv\lib\site-packages\kivy\uix\widget.py", line 470, in apply_class_lang_rules Builder.apply( File "C:\Users\nicho\PycharmProjects\Temasek Polytechnic MP (Software App)\venv\lib\site-packages\kivy\lang\builder.py", line 540, in apply self._apply_rule( File "C:\Users\nicho\PycharmProjects\Temasek Polytechnic MP (Software App)\venv\lib\site-packages\kivy\lang\builder.py", line 736, in _apply_rule raise BuilderException( kivy.lang.builder.BuilderException: Parser: File "C:\Users\nicho\PycharmProjects\Temasek Polytechnic MP (Software App)\Test Integration.kv", line 17: ... 15: font_size: 24 16: on_release: >> 17: app.root.current = "Checklist" >> 18: root.manager.transition = "left" >> 19: >> ... >> AttributeError: release >> File "C:\Users\nicho\PycharmProjects\Temasek Polytechnic MP (Software App)\venv\lib\site-packages\kivy\lang\builder.py", line 729, in _apply_rule >> raise AttributeError(key) Process finished with exit code 1
问题修复方案
1. 解决AttributeError: release核心问题
KV文件中给Label控件绑定了on_release事件,但Label不支持该事件,只有Button、ToggleButton等可交互控件才有on_release。将触发界面切换的Label替换为Button:
# 原代码 Label: text: "Safety Checklist" font_size: 24 on_release: app.root.current = "Checklist" root.manager.transition = "left" # 修改后 Button: text: "进入安全清单" font_size: 24 on_release: app.root.current = "Checklist" root.manager.transition.direction = "left"
2. 修正KV文件结构错误
原KV文件把三个Screen的内容全部塞进<FirstWindow>,还重复定义name属性,导致ScreenManager无法识别独立窗口。需要分别定义每个Screen:
WindowManager: FirstWindow: SecondWindow: ThirdWindow: <FirstWindow>: name: "Main Menu" BoxLayout: orientation: "vertical" size: root.width, root.height # 主菜单按钮 Button: text: "进入安全清单" font_size: 24 on_release: app.root.current = "Checklist" root.manager.transition.direction = "left" Button: text: "填写我们的调查!" font_size: 20 on_release: import webbrowser webbrowser.open('https://forms.gle/k6cfYEU1snzpykxY9') <SecondWindow>: name: "Test Button" # 此处可添加第二个窗口的UI内容 <ThirdWindow>: name: "Checklist" BoxLayout: orientation: "vertical" size: root.width, root.height # 清单内容...
3. 修复屏幕过渡设置错误
原代码root.manager.transition = "left"写法错误,需设置过渡方向属性:
root.manager.transition.direction = "left"
4. 添加缺失的复选框处理方法
KV文件中调用了root.checkbox_click,但ThirdWindow类未定义该方法,需在Python代码中补充:
class ThirdWindow(Screen): def checkbox_click(self, checkbox, value, item): if value: self.ids.output_label.text = f"已确认拥有:{item}" else: self.ids.output_label.text = f"未确认拥有:{item}"
内容的提问来源于stack exchange,提问作者Nicholas
相关产品推荐
相关产品推荐

