📜  Python| Kivy 中的 StackLayout

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

Python| Kivy 中的 StackLayout

Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。

堆栈布局:

要使用 StackLayout 首先通过以下命令导入 StackLayout:

from kivy.uix.stacklayout import StackLayout

理解 Stack 和 Boxlayout 之间的区别非常令人困惑。
StackLayout可以比 BoxLayout 更复杂地组织小部件 Boxlayout 可以以垂直或水平方式组织小部件。但是使用 StackLayout,您可以组合方向。有 4 行方向和 4 列方向。

More flexible that Boxlayout(1D)

StackLayout Orientation (2D):
 - right to left or left to right
 - top to bottom or bottom to top
 - 'rl-bt', 'rl-tb', lr-bt', 'lr-tb'(Row wise)
 - 'bt-rl', 'bt-lr', 'tb-rl', 'tb-lr'(Column wise)

四个行方向和四个列方向如下图所示。

Basic Approach to create Stack layout :

1) import kivy
2) import kivyApp
3) import Button
4) import Stacklayout
5) Set minimum version(optional)
6) create App class
7) return widget
8) Run an instance of the class


方法的实施:

Python3
# code to show how to use StackLayout
 
# import kivy module
import kivy
 
# this restricts the kivy version i.e
# below this kivy version you cannot
# use the app or software
kivy.require("1.9.1")
   
# 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 StackLayout arranges children vertically
# or horizontally, as many as the layout can fit.
from kivy.uix.stacklayout import StackLayout
 
 
# class in which we are creating StackLayout
class StackLayoutApp(App):
       
    def build(self):
 
        # Different orientation
        # ['lr-tb', 'tb-lr', 'rl-tb', 'tb-rl',
           'lr-bt', 'bt-lr', 'rl-bt', 'bt-rl']       
        SL = StackLayout(orientation ='lr-tb')
 
        # Creating Multiple Buttons
        btn1 = Button(text ="B1",
                      font_size = 20,
                      size_hint =(.2, .1))
        btn2 = Button(text ="B2",
                      font_size = 20,
                      size_hint =(.2, .1))
        btn3 = Button(text ="B3",
                      font_size = 20,
                      size_hint =(.2, .1))
        btn4 = Button(text ="B4",
                      font_size = 20,
                      size_hint =(.2, .1))
        btn5 = Button(text ="B5",
                      font_size = 20,
                      size_hint =(.2, .1))
        btn6 = Button(text ="B6",
                      font_size = 20,
                      size_hint =(.2, .1))
        btn7 = Button(text ="B7",
                      font_size = 20,
                      size_hint =(.2, .1))
        btn8 = Button(text ="B8",
                      font_size = 20,
                      size_hint =(.2, .1))
        btn9 = Button(text ="B9",
                      font_size = 20,
                      size_hint =(.2, .1))
        btn10 = Button(text ="B10",
                      font_size = 20,
                      size_hint =(.2, .1))
 
 
        # adding widgets
        SL.add_widget(btn1)
        SL.add_widget(btn2)
        SL.add_widget(btn3)
        SL.add_widget(btn4)
        SL.add_widget(btn5)
        SL.add_widget(btn6)
        SL.add_widget(btn7)
        SL.add_widget(btn8)
        SL.add_widget(btn9)
        SL.add_widget(btn10)
 
 
        # returning widgets
        return SL
 
# run function runs the whole program
# i.e run() method which calls the
# target function passed to the constructor.
if __name__ == '__main__':
    StackLayoutApp().run()


输出:

这是针对“lr-tb”方向的。首先,小部件从左到右添加,然后从上到下添加。
注意:如果要更改方向,只需将第 31 行中的方向更改为以下任何方向 -

For row wise orientation use:
  -'lr-tb'
  -'lr-bt'
  -'rl-tb'
  -'rl-bt'

For column wise orientation use:
  -'tb-lr'
  -'tb-rl'
  -'bt-lr'
  -'bt-rl'


下面是上面所有方向的图片输出——
对于行方向使用:

'lr-tb'

输出:

'lr-bt'

输出:

'rl-tb'

输出:

'rl-bt'

输出:


对于列方向使用:

'tb-lr'

输出:

'tb-rl'

输出:

'bt-lr'

输出:

'bt-rl'

输出: