Kivy-创建新的小部件并设置其位置和大小 [英] Kivy - Create new widget and set its position and size

查看:125
本文介绍了Kivy-创建新的小部件并设置其位置和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个小问题.因此,我只是尝试创建自己的小部件,而我成功了,只不过是正确设置了它的大小和位置(与其父级相同).

got a little problem. So Iam trying to create my own widget, and i have succeed, only except for setting its size and position right(to be same as its parrent).

class Story(App):
    def build(self):    
        return MyWidgets().init()

该应用程序具有GridLayout作为持有人,我想将StoryWidget传递到其中

The app has GridLayout as a holder, into which i want to pass the StoryWidget

class MyWidgets(object):

    def init(self):
        root = GridLayout(cols=2,rows=1)
        root.add_widget(StoryWidget())
        root.add_widget(Button())
        return root

Story Widget的用法如下:

Story Widget goes as this:

class StoryWidget(Widget):
    def __init__(self,**kwargs):
        super(StoryWidget, self).__init__(**kwargs)
        topLayout=BoxLayout(orientation = "vertical")
        topLayout.add_widget(Button(text="first"))
        topLayout.add_widget(Button(text="second"))
        self.add_widget(topLayout)

如果我尝试使用背景色,则效果很好:

If I try to get the background color to it, it works fine:

        with self.canvas.before:
            Color(.9,.9,1)  
            self.Backgroud = Rectangle(pos=self.pos,size=self.size)

        self.bind(pos=self.repaint,size=self.repaint)
        self.bind(pos=self.resize,size=self.resize)

    def repaint(self,*args):
        self.Backgroud.pos = self.pos
        self.Backgroud.size = self.size

root(Gridlayout)的整个第一列都正确地重新粉刷成白色, 但是该小部件位于defaut pos(0,0)和默认大小(100,100)上. 据我所知,这是因为Widget无法处理这些事情.布局应该以某种方式自动执行.可以看出,StoryWidget的根小部件是布局.我不知道为什么它不起作用.我尝试从Layout继承而不是从Widget继承,但仍然没有.有什么建议吗?谢谢!

The whole firs column of root(Gridlayout) gets correctly repainted in white, but the Widget stands on defaut pos(0,0) and default size(100,100). From what i know, its because Widget cant handle these things. Layout should do it automatically somehow. As can be seen, the root widget of StoryWidget is layout. I do not know why it`s not working. I tried to inherit from the Layout instead of Widget, but still nothing. Any advice? Thanks!

推荐答案

好吧,我已经弄清楚了,原来我忘了设置适当的属性.所以Iam现在使用Gridlayout而不是BoxLayout,在这种情况下,它需要cols和rows,因此现在看起来应该像这样:

Alright, i have figured it out, turns out i forgot to set appropriate attributes. So Iam now using Gridlayout instead of BoxLayout, in which case it needs cols and rows so it should now look like this:

class StoryWidget(GridLayout):
    def __init__(self,**kwargs):
        self.cols=1
        self.rows=1
        super(StoryWidget, self).__init__(**kwargs)
        topLayout=BoxLayout(orientation = "vertical")
        topLayout.add_widget(Button(text="first"))
        topLayout.add_widget(Button(text="second"))
        self.add_widget(topLayout)

    with self.canvas.before:
        Color(.9,.9,1)  
        self.Backgroud = Rectangle(pos=self.pos,size=self.size)

    self.bind(pos=self.repaint,size=self.repaint)
    self.bind(pos=self.resize,size=self.resize)

这篇关于Kivy-创建新的小部件并设置其位置和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆