Adding widget results in "AttributeError: 'HomeScreen' object has no attribute '_trigger_layout'"
Adding widget results in "AttributeError: 'HomeScreen' object has no attribute '_trigger_layout'"
为什么这段代码会产生以下错误?
AttributeError: 'HomeScreen' object has no attribute '_trigger_layout'
class HomeScreen(BoxLayout):
def __init__(self):
print "homescreen"
topbar = BoxLayout(spacing = -2, size = (64, 64), size_hint = (1, None))
back_button = Button(size = (32, 64), size_hint = (None, 1), text = '>', font_size = 15 + 0.015 * self.height, background_color = (0.239, 0.815, 0.552, 1))
home_button = Button(text = "HOME", font_size = 10 + 0.015 * self.width, background_color = (0.239, 0.815, 0.552, 1))
more_button = Button(size = (32, 64), size_hint = (None, 1), text = '...', font_size = 15 + 0.015 * self.height, background_color = (0.239, 0.815, 0.552, 1))
topbar.add_widget(back_button)
topbar.add_widget(home_button)
topbar.add_widget(more_button)
self.add_widget(topbar)
因为你覆盖了需要的init方法。在下面的代码中我也覆盖了init方法,但是我也是在覆盖之前调用了init方法。
class HomeScreen(BoxLayout):
def __init__(self, **kwargs):
super(HomeScreen, self).__init__(**kwargs)
...
from kivy.app import App
from kivy.uix.slider import Slider
from kivy.uix.treeview import TreeView, TreeViewNode
from kivy.uix.button import Button
from kivy.uix.slider import Slider
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class HomeScreen(BoxLayout):
def __init__(self, **kwargs):
super(HomeScreen, self).__init__(**kwargs)
print("homescreen")
topbar = BoxLayout(spacing = 0, size = (64, 64), size_hint = (1, None))
back_button = Button(size = (32, 64), size_hint = (None, 1), text = '>', font_size = 15 + 0.015 * self.height, background_color = (0.239, 0.815, 0.552, 1))
home_button = Button(text = "HOME", font_size = 10 + 0.015 * self.width, background_color = (0.239, 0.815, 0.552, 1))
more_button = Button(size = (32, 64), size_hint = (None, 1), text = '...', font_size = 15 + 0.015 * self.height, background_color = (0.239, 0.815, 0.552, 1))
topbar.add_widget(back_button)
topbar.add_widget(home_button)
topbar.add_widget(more_button)
self.add_widget(topbar)
class TestApp(App):
def build(self):
return HomeScreen()
if __name__ == '__main__':
TestApp().run()
为什么这段代码会产生以下错误?
AttributeError: 'HomeScreen' object has no attribute '_trigger_layout'
class HomeScreen(BoxLayout):
def __init__(self):
print "homescreen"
topbar = BoxLayout(spacing = -2, size = (64, 64), size_hint = (1, None))
back_button = Button(size = (32, 64), size_hint = (None, 1), text = '>', font_size = 15 + 0.015 * self.height, background_color = (0.239, 0.815, 0.552, 1))
home_button = Button(text = "HOME", font_size = 10 + 0.015 * self.width, background_color = (0.239, 0.815, 0.552, 1))
more_button = Button(size = (32, 64), size_hint = (None, 1), text = '...', font_size = 15 + 0.015 * self.height, background_color = (0.239, 0.815, 0.552, 1))
topbar.add_widget(back_button)
topbar.add_widget(home_button)
topbar.add_widget(more_button)
self.add_widget(topbar)
因为你覆盖了需要的init方法。在下面的代码中我也覆盖了init方法,但是我也是在覆盖之前调用了init方法。
class HomeScreen(BoxLayout):
def __init__(self, **kwargs):
super(HomeScreen, self).__init__(**kwargs)
...
from kivy.app import App
from kivy.uix.slider import Slider
from kivy.uix.treeview import TreeView, TreeViewNode
from kivy.uix.button import Button
from kivy.uix.slider import Slider
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class HomeScreen(BoxLayout):
def __init__(self, **kwargs):
super(HomeScreen, self).__init__(**kwargs)
print("homescreen")
topbar = BoxLayout(spacing = 0, size = (64, 64), size_hint = (1, None))
back_button = Button(size = (32, 64), size_hint = (None, 1), text = '>', font_size = 15 + 0.015 * self.height, background_color = (0.239, 0.815, 0.552, 1))
home_button = Button(text = "HOME", font_size = 10 + 0.015 * self.width, background_color = (0.239, 0.815, 0.552, 1))
more_button = Button(size = (32, 64), size_hint = (None, 1), text = '...', font_size = 15 + 0.015 * self.height, background_color = (0.239, 0.815, 0.552, 1))
topbar.add_widget(back_button)
topbar.add_widget(home_button)
topbar.add_widget(more_button)
self.add_widget(topbar)
class TestApp(App):
def build(self):
return HomeScreen()
if __name__ == '__main__':
TestApp().run()