有点问题.所以我正在尝试创建自己的小部件,并且我成功了,只是将其大小和位置设置为正确(与其父级相同).
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) 的整个第一列被正确地重新绘制为白色,但小部件位于默认位置(0,0)和默认大小(100,100).据我所知,这是因为 Widget 无法处理这些事情.布局应该以某种方式自动完成.可以看出,StoryWidget 的根小部件是 layout.我不知道为什么它不起作用.我试图从 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!
好吧,我想通了,原来我忘了设置适当的属性.所以我现在使用 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 - 创建新的小部件并设置其位置和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!