Python Kivy在引用变量时显示错误
我阅读并尝试为我的应用程序实现关闭事件 我用Python用Kivy编写了以下代码Python Kivy在引用变量时显示错误,python,kivy,Python,Kivy,我阅读并尝试为我的应用程序实现关闭事件 我用Python用Kivy编写了以下代码 #-*- coding: utf-8 -*- from kivy.lang import Builder Builder.load_string(""" <TextWidget>: BoxLayout: size: root.size Button: text: "Test App" """) from kivy.app import
#-*- coding: utf-8 -*-
from kivy.lang import Builder
Builder.load_string("""
<TextWidget>:
BoxLayout:
size: root.size
Button:
text: "Test App"
""")
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.properties import StringProperty
class TextWidget(Widget):
def __init__(self, **kwargs):
super(TextWidget, self).__init__(**kwargs)
self.text = 'test'
def on_request_close(self, *args):
print(self.text)
class TestApp(App):
def __init__(self, **kwargs):
super(TestApp, self).__init__(**kwargs)
def build(self):
scn = TextWidget
Window.bind(on_request_close=scn.on_request_close)
return TextWidget()
if __name__ == '__main__':
TestApp().run()
如何解决此问题?您正在尝试访问类本身,而不是具有
text
属性的TextWidget实例
您可以在TextWidget
的\uuuu init\uuuu
方法中进行绑定以修复它(如下),或者您可以通过为实例分配一个id来访问它
类文本小部件(小部件):
定义初始(自我,**kwargs):
超级(TextWidget,self)。\uuuuu初始化(**kwargs)
绑定(请求时关闭=self.on请求时关闭)
self.text='test'
def on_请求_关闭(self,*args):
app=self.text
打印(应用程序)
类TestApp(应用程序):
def生成(自):
返回TextWidget()
AttributeError: 'WindowSDL' object has no attribute 'text'