0

我有一个非常简单的带有单个按钮的窗口案例。通过按钮释放,我想弹出一个带有一些文本的模态视图。在每个按钮释放中,我创建并打开一个实例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_hintsize

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()

ModalViewLabel位置完全搞砸了。我尝试使用anchor_x并尝试修复标签位置,但没有运气anchor_yModalView2我究竟做错了什么?

4

1 回答 1

0

ModalView已有的和size_hint属性size,所以你不需要创建新的,只需将现有的属性设置为你想要的值:

class ModalView2(ModalView):

    def __init__(self, **kwargs):
        super(ModalView2, self).__init__(**kwargs)
        self.size_hint = (None, None)
        self.size = (200, 200)
于 2020-05-06T14:09:40.540 回答