我有一个非常简单的带有单个按钮的窗口案例。通过按钮释放,我想弹出一个带有一些文本的模态视图。在每个按钮释放中,我创建并打开一个实例ModalView
并且它可以工作:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.modalview import ModalView
from kivy.uix.label import Label
class AButton(Button):
def on_release(self, *largs):
view = ModalView(size_hint=(None, None), size=[200, 200])
view.add_widget(Label(text='I am a modal view'))
view.open()
class MyApp(App):
def build(self):
return AButton()
if __name__ == '__main__':
MyApp().run()
现在假设我想创建一个子类,这样我就ModalView
不必每次弹出一个模态视图时都指定。这是更改后的代码:size_hint
size
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.modalview import ModalView
from kivy.uix.label import Label
from kivy.properties import ListProperty
class AButton(Button):
def on_release(self, *largs):
view = ModalView2()
view.add_widget(Label(text='I am a modal view'))
view.open()
class ModalView2(ModalView):
size_hint = ListProperty([None, None])
size = ListProperty([200, 200])
def __init__(self, **kwargs):
super(ModalView2, self).__init__(**kwargs)
class MyApp(App):
def build(self):
return AButton()
if __name__ == '__main__':
MyApp().run()
ModalView
和Label
位置完全搞砸了。我尝试使用anchor_x
并尝试修复标签位置,但没有运气anchor_y
。ModalView2
我究竟做错了什么?