Kivy - Create new widget and set its position and size(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) 的整个第一列被正确地重新绘制为白色,但小部件位于默认位置(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 - 创建新的小部件并设置其位置和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Kivy - 创建新的小部件并设置其位置和大小


基础教程推荐
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01