简体   繁体   English

如何使小部件在kivy中的gridlayout中跨越多个列/行

[英]How to make a widget span multiple columns/rows in gridlayout in kivy

I would like to create an app in kivy with widgets in a grid and I want to be able to set some widgets to be larger - to occupy more than one cell in a grid. 我想在网格中使用小部件在kivy中创建一个应用程序,我希望能够将一些小部件设置得更大 - 占用网格中的多个单元格。

GridLayout seems the most appropriate, but it seems to not support placing widgets in more than one cell. GridLayout似乎是最合适的,但似乎不支持将小部件放在多个单元格中。

To be more specific I want behavour similar to the grid geometry manager from Tkinter, when setting columnspan or rowspan to more than 1. 更具体地说,当将columnspanrowspan设置为大于1时,我希望行为类似于Tkinter的网格几何管理器。

Like this: 像这样:

(widget)(widget)(widget)
( bigger widget )(widget)
...

I would prefer to be able to do this using existing kivy layouts instead of having to write my own layout class to handle this, but if no other option is possible, this is also ok. 我希望能够使用现有的kivy布局来实现这一点,而不是必须编写我自己的布局类来处理这个问题,但如果没有其他选项可行,这也没关系。

Another option here is to have a GridLayout with 1 column and populate each row with a BoxLayout with orientation="horizontal". 这里的另一个选择是让GridLayout具有1列,并使用方向=“水平”的BoxLayout填充每一行。 You can then format each BoxLayout (row) as you want. 然后,您可以根据需要格式化每个BoxLayout(行)。

For more info on the BoxLayout: http://kivy.org/docs/api-kivy.uix.boxlayout.html 有关BoxLayout的更多信息: http//kivy.org/docs/api-kivy.uix.boxlayout.html

I don't think a GridLayout is really suitable, it just isn't designed for quite that usage. 我不认为GridLayout真的适合,它只是不适合那种用法。

If I personally had to do this, I'd probably make my own Layout class, it wouldn't need a very complicated do_layout method. 如果我个人不得不这样做,我可能会创建自己的Layout类,它不需要非常复杂的do_layout方法。

One possible start point would be the SparseGridLayout I made a while ago. 一个可能的起点是我前一段时间制作的SparseGridLayout You could very easily add column and row span properties to it...actually, I'll probably add them myself now that you've given me the idea! 您可以非常轻松地向其添加列和行范围属性...实际上,我可能会自己添加它们,因为您已经给了我这个想法!

That might not be ideal if you have a big grid full of widgets, in which case something similar to a gridlayout might be better, or possibly a combination of multiple layouts if the spanning widgets are in a particular pattern. 如果你有一个充满小部件的大网格,这可能不是理想的,在这种情况下类似于gridlayout的东西可能更好,或者如果跨越小部件是特定模式的话,可能是多个布局的组合。

Lets assume you have a GridLayout with two columns and you want to span the first row .You can add Two FloatLayout whereby the first FloatLayout will contain the widget you would like to span while the second Layout will have row and height values to zero.This would archive a span effect 让我们假设您有一个包含两columnsGridLayout ,并且您希望跨越第一row 。您可以添加两个FloatLayout ,其中第一个FloatLayout将包含您想要span的小部件,而第二个Layoutrowheight值设置为零。将存档跨度效果

here is an example of .kv span effect 这是.kv span效应的一个例子

GridLayout:
     cols:2

     FloatLayout:   # The first FloatLayout in the first column in the gridLayout
        size_hint:None,None
        size: 0,50

        BoxLayout:
           size_hint: None,None
           size: root.width-40,50
           pos_hint: {'x':.5,'center_y':.5}

           BoxLayout:
              padding:0,0,5,0

              Label:
                 id:lbl_unknown
                 text:'Accession number :'

              TextInput:
                 text:''

     FloatLayout:  # The second FloatLayout in the second column of the gridLayout
         size_hint:None,None
         size:0,0

     Label:
        text:'Label 1:'
     TextInput:

     Label:
        text:'Label 2:'
     TextInput:

An easy work around is to pack BoxLayout objects into any type of parent layout you want using your better judgement to decide the orientation of each subsequent BoxLayout 一个简单的解决方法是将BoxLayout对象打包成您想要的任何类型的父布局,使用您更好的判断来决定每个后续BoxLayout的方向

(decided to use box layouts for almost all of my own project over any gridlayout) (决定在任何gridlayout上使用几乎所有项目的框布局)

This is my class: 这是我的班级:

class AuthPage(BoxLayout):
    def __init__(self, **kwargs):
        super(AuthPage, self).__init__(**kwargs)

        self.orientation = 'vertical'

        self.add_widget(Label(text='Authenticate'))

        unameRow = BoxLayout(orientation='horizontal')
        unameRow.add_widget(Label(text='User Name'))
        unameRow.username = TextInput(multiline=False)
        unameRow.add_widget(unameRow.username)
        self.add_widget(unameRow)

        pwordRow = BoxLayout(orientation='horizontal')
        pwordRow.add_widget(Label(text='password'))
        pwordRow.password = TextInput(password=True, multiline=False)
        pwordRow.add_widget(pwordRow.password)
        self.add_widget(pwordRow)

        self.add_widget(Button(text='authenticate'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM