📌  相关文章
📜  Python|没有 .kv 文件的 Kivy 中的网格布局

📅  最后修改于: 2022-05-13 01:54:23.548000             🧑  作者: Mango

Python|没有 .kv 文件的 Kivy 中的网格布局

Kivy 是一个独立于平台的平台,可以在 Android、IOS、linux 和 Windows 等平台上运行。Kivy 为您提供了一次编写代码并在不同平台上运行的功能。基本上是用来开发Android应用的,但不代表不能用在Desktops应用上。

网格布局:

  • 小部件必须放置在特定的列/行中。每个孩子都会自动分配一个位置,该位置由布局配置和孩子在孩子列表中的索引决定。
  • 网格布局必须始终包含以下任一输入约束:
    GridLayout.cols 或 GridLayout.rows。如果不指定 cols 或 rows,则 Layout 将抛出异常。
  • GridLayout 将子元素排列在矩阵中。它占用可用空间并将其划分为列和行,然后将小部件添加到生成的“单元格”。
  • 行和列就像我们在矩阵中观察到的一样,在这里我们可以调整每个网格的大小。
  • 初始大小由 col_default_width 和 row_default_height 属性给出。我们可以通过设置 col_force_default 或 row_force_default 属性来强制使用默认大小。这将强制布局忽略子项的 width 和 size_hint 属性并使用默认大小。

要使用 GridLayout,我们需要做的第一件事就是导入它。

from kivy.uix.gridlayout import GridLayout 

创建 GridLayout 的基本方法:

1) import kivy
2) import kivyApp
3) import button
4) import Gridlayout
5) Set minimum version(optional)
6) create App class:
          - define build function
              : add widget (Buttons)
7) return Layout/widget/Class(according to requirement)
8) Run an instance of the class


方法的实施——
代码#1:
在下面的示例中,所有小部件都将具有相同的大小。默认情况下,size_hint 为 (1, 1),因此 Widget 将采用父级的完整大小:

Python3
# Sample Python application demonstrating 
# How to create GridLayout in Kivy
 
# import kivy module
import kivy
   
# base Class of your App inherits from the App class.  
# app:always refers to the instance of your application  
from kivy.app import App
   
# creates the button in kivy 
# if not imported shows the error 
from kivy.uix.button import Button
 
# The GridLayout arranges children in a matrix.
# It takes the available space and
# divides it into columns and rows,
# then adds widgets to the resulting “cells”.
from kivy.uix.gridlayout import GridLayout
 
# creating the App class
class Grid_LayoutApp(App):
 
    # to build the application we have to
    # return a widget on the build() function.
    def build(self):
 
        # adding GridLayouts in App
        # Defining number of column
        # You can use row as well depends on need
        layout = GridLayout(cols = 2)
 
        # 1st row
        layout.add_widget(Button(text ='Hello 1'))
        layout.add_widget(Button(text ='World 1'))
 
        # 2nd row
        layout.add_widget(Button(text ='Hello 2'))
        layout.add_widget(Button(text ='World 2'))
 
        # 3rd row
        layout.add_widget(Button(text ='Hello 3'))
        layout.add_widget(Button(text ='World 3'))
 
        # 4th row
        layout.add_widget(Button(text ='Hello 4'))
        layout.add_widget(Button(text ='World 4'))
 
        # returning the layout
        return layout
 
# creating object of the App class
root = Grid_LayoutApp()
# run the App
root.run()


Python3
# creating the App class
class Grid_LayoutApp(App):
 
    # to build the application we have to
    # return a widget on the build() function.
    def build(self):
 
        # adding GridLayouts in App
        # Defining number of column
        # You can use row as well depends on need
        layout = GridLayout(cols = 2)
 
        # 1st row
        layout.add_widget(Button(text ='Hello 1', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 1'))
 
        # 2nd row
        layout.add_widget(Button(text ='Hello 2', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 2'))
 
        # 3rd row
        layout.add_widget(Button(text ='Hello 3', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 3'))
 
        # 4th row
        layout.add_widget(Button(text ='Hello 4', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 4'))
 
        # returning the layout
        return layout


Python3
# creating the App class
class Grid_LayoutApp(App):
 
    # to build the application we have to
    # return a widget on the build() function.
    def build(self):
 
        # adding GridLayouts in App
        # Defining number of column and size of the buttons i.e height
        layout = GridLayout(cols = 2, row_force_default = True,
                            row_default_height = 30)
 
        # 1st row
        layout.add_widget(Button(text ='Hello 1', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 1'))
 
        # 2nd row
        layout.add_widget(Button(text ='Hello 2', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 2'))
 
        # 3rd row
        layout.add_widget(Button(text ='Hello 3', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 3'))
 
        # 4th row
        layout.add_widget(Button(text ='Hello 4', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 4'))
 
        # returning the layout
        return layout


输出:

现在只需用代码#2 和代码#3 更改上面代码中的类代码,除此之外,所有代码都将与代码#1 相同,并在更改后运行代码,您将得到以下结果。
代码#2:
现在,让我们将 Hello 按钮的大小固定为 100px,而不是使用 size_hint_x=1:

Python3

# creating the App class
class Grid_LayoutApp(App):
 
    # to build the application we have to
    # return a widget on the build() function.
    def build(self):
 
        # adding GridLayouts in App
        # Defining number of column
        # You can use row as well depends on need
        layout = GridLayout(cols = 2)
 
        # 1st row
        layout.add_widget(Button(text ='Hello 1', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 1'))
 
        # 2nd row
        layout.add_widget(Button(text ='Hello 2', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 2'))
 
        # 3rd row
        layout.add_widget(Button(text ='Hello 3', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 3'))
 
        # 4th row
        layout.add_widget(Button(text ='Hello 4', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 4'))
 
        # returning the layout
        return layout

输出:

代码#3:
现在,让我们将行高固定为特定大小:

Python3

# creating the App class
class Grid_LayoutApp(App):
 
    # to build the application we have to
    # return a widget on the build() function.
    def build(self):
 
        # adding GridLayouts in App
        # Defining number of column and size of the buttons i.e height
        layout = GridLayout(cols = 2, row_force_default = True,
                            row_default_height = 30)
 
        # 1st row
        layout.add_widget(Button(text ='Hello 1', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 1'))
 
        # 2nd row
        layout.add_widget(Button(text ='Hello 2', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 2'))
 
        # 3rd row
        layout.add_widget(Button(text ='Hello 3', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 3'))
 
        # 4th row
        layout.add_widget(Button(text ='Hello 4', size_hint_x = None, width = 100))
        layout.add_widget(Button(text ='World 4'))
 
        # returning the layout
        return layout

输出:


参考: https://kivy.org/doc/stable/api-kivy.uix.gridlayout.html