<bdo id='LdfSb'></bdo><ul id='LdfSb'></ul>
  • <legend id='LdfSb'><style id='LdfSb'><dir id='LdfSb'><q id='LdfSb'></q></dir></style></legend>

    <small id='LdfSb'></small><noframes id='LdfSb'>

      <i id='LdfSb'><tr id='LdfSb'><dt id='LdfSb'><q id='LdfSb'><span id='LdfSb'><b id='LdfSb'><form id='LdfSb'><ins id='LdfSb'></ins><ul id='LdfSb'></ul><sub id='LdfSb'></sub></form><legend id='LdfSb'></legend><bdo id='LdfSb'><pre id='LdfSb'><center id='LdfSb'></center></pre></bdo></b><th id='LdfSb'></th></span></q></dt></tr></i><div id='LdfSb'><tfoot id='LdfSb'></tfoot><dl id='LdfSb'><fieldset id='LdfSb'></fieldset></dl></div>
      <tfoot id='LdfSb'></tfoot>
      1. Kivy - 创建新的小部件并设置其位置和大小

        时间:2023-10-09

        <small id='fS7W7'></small><noframes id='fS7W7'>

      2. <legend id='fS7W7'><style id='fS7W7'><dir id='fS7W7'><q id='fS7W7'></q></dir></style></legend>

            1. <i id='fS7W7'><tr id='fS7W7'><dt id='fS7W7'><q id='fS7W7'><span id='fS7W7'><b id='fS7W7'><form id='fS7W7'><ins id='fS7W7'></ins><ul id='fS7W7'></ul><sub id='fS7W7'></sub></form><legend id='fS7W7'></legend><bdo id='fS7W7'><pre id='fS7W7'><center id='fS7W7'></center></pre></bdo></b><th id='fS7W7'></th></span></q></dt></tr></i><div id='fS7W7'><tfoot id='fS7W7'></tfoot><dl id='fS7W7'><fieldset id='fS7W7'></fieldset></dl></div>
                <tbody id='fS7W7'></tbody>
                <bdo id='fS7W7'></bdo><ul id='fS7W7'></ul>
                <tfoot id='fS7W7'></tfoot>
                  本文介绍了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 - 创建新的小部件并设置其位置和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:BoxLayout 中的两个 kivy 图像 下一篇:un_active 按钮,直到函数在 kivy 中完成

                  相关文章

                  最新文章

                      <small id='JHDft'></small><noframes id='JHDft'>

                      • <bdo id='JHDft'></bdo><ul id='JHDft'></ul>
                      <tfoot id='JHDft'></tfoot>
                    1. <legend id='JHDft'><style id='JHDft'><dir id='JHDft'><q id='JHDft'></q></dir></style></legend>
                      <i id='JHDft'><tr id='JHDft'><dt id='JHDft'><q id='JHDft'><span id='JHDft'><b id='JHDft'><form id='JHDft'><ins id='JHDft'></ins><ul id='JHDft'></ul><sub id='JHDft'></sub></form><legend id='JHDft'></legend><bdo id='JHDft'><pre id='JHDft'><center id='JHDft'></center></pre></bdo></b><th id='JHDft'></th></span></q></dt></tr></i><div id='JHDft'><tfoot id='JHDft'></tfoot><dl id='JHDft'><fieldset id='JHDft'></fieldset></dl></div>