个性化阅读
专注于IT技术分析

Python Kivy中的StackLayout用法示例

Kivy是Python中与平台无关的GUI工具。由于它可以在Android, IOS, Linux和Windows等操作系统上运行。它基本上是用于开发Android应用程序, 但这并不意味着它不能在桌面应用程序上使用。

??Kivy教程–通过示例学习Kivy。

StackLayout:

要使用StackLayout firt, 请通过以下命令导入StackLayout:

from kivy.uix.stacklayout import StackLayout

Stack和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

该方法的实现:

# 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()

输出如下:

Python Kivy中的StackLayout1

这是针对方向”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'

输出如下:

Python Kivy中的StackLayout2
'lr-bt'

输出如下:

Python Kivy中的StackLayout3
'rl-tb'

输出如下:

Python Kivy中的StackLayout4
'rl-bt'

输出如下:

Python Kivy中的StackLayout5

对于列方向定向, 请使用:

'tb-lr'

输出如下:

Python Kivy中的StackLayout6
'tb-rl'

输出如下:

Python Kivy中的StackLayout7
'bt-lr'

输出如下:

Python Kivy中的StackLayout8
'bt-rl'

输出如下:

Python Kivy中的StackLayout9

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


赞(0)
未经允许不得转载:srcmini » Python Kivy中的StackLayout用法示例

评论 抢沙发

评论前必须登录!