Python| Kivy 中的布局(多个布局)中的布局
Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。
在本文中,我们将讨论如何在布局中使用布局,基本上是多个布局的最基本示例。
大多数时候,我们可以使用一种布局,但很难使用多种布局。
Kivy Tutorial – Learn Kivy with Examples.
在 kivy 中有许多类型的布局:
- AnchorLayout:小部件可以锚定到'top'、'bottom'、'left'、'right'或'center'。
- BoxLayout:小部件按“垂直”或“水平”方向顺序排列。
- FloatLayout:小部件本质上是不受限制的。
- 相对布局:子小部件相对于布局定位。
- GridLayout:小部件排列在由 rows 和 cols 属性定义的网格中。
- PageLayout :用于创建简单的多页布局,以允许使用边框轻松从一页翻转到另一页的方式。
- ScatterLayout:小部件的定位类似于RelativeLayout,但它们可以平移、旋转和缩放。
- StackLayout:小部件以 lr-tb(从左到右然后从上到下)或 tb-lr 顺序堆叠。
注意:您可以在单个文件中使用尽可能多的文件。
在一个文件中创建多个布局的基本方法:
1) import kivy
2) import kivyApp
3) import BoxLayout
4) import
4) set minimum version(optional)
5) Extend the container class
6) set up .kv file :
7) create App class
8) return container class or layout
9) Run an instance of the class
方法的实施——
main.py 文件
笔记:
如果您使用 .kv 文件制作多个布局 无需导入 Gridlayout、Boxlayout、AnchorLayout、FloatLayout、StackLayout、PageLayout、Button 等。因为 .kv 文件支持所有这些,因为它已经导入了所有这些。但如果在没有 .kv 文件的情况下执行此操作,则必须导入这些文件。
Python3
## Sample Python application demonstrating the
## Program of How to use Multiple Layouts in Single file
########################################################################
# 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
# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
kivy.require('1.9.0')
# creates the button in kivy
# if not imported shows the error
from kivy.uix.button import Button
# BoxLayout arranges children in a vertical or horizontal box.
# or help to put the childrens at the desired location.
from kivy.uix.boxlayout import BoxLayout
# 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
# The PageLayout class is used to create
# a simple multi-page layout,
# in a way that allows easy flipping from
# one page to another using borders.
from kivy.uix.pagelayout import PageLayout
########################################################################
# creating the root widget used in .kv file
class MultipleLayout(PageLayout):
pass
########################################################################
# creating the App class in which name
#.kv file is to be named PageLayout.kv
class Multiple_LayoutApp(App):
# defining build()
def build(self):
# returning the instance of root class
return MultipleLayout()
########################################################################
# creating object of Multiple_LayoutApp() class
MlApp = Multiple_LayoutApp()
# run the class
MlApp.run()
Python3
# Program of How to use Multiple Layouts in Single .kv file
########################################################################
# creating page Layout
:
#########################################################################
# Creating Page 1
# Using BoxLayout inside PageLayout
BoxLayout:
# creating Canvas
canvas:
Color:
rgba: 216 / 255., 195 / 255., 88 / 255., 1
Rectangle:
pos: self.pos
size: self.size
# Providing orientation to the BoxLayout
orientation: 'vertical'
# Adding Label to Page 1
Label:
size_hint_y: None
height: 1.5 * self.texture_size[1]
text: 'page 1'
# Creating Button
Button:
text: 'GFG :)'
# Adding On_press function
# i.e binding function to press / touch
on_press: print("This Is The First Page")
#########################################################################
# Creating Page 2
BoxLayout:
orientation: 'vertical'
canvas:
Color:
rgba: 109 / 255., 8 / 255., 57 / 255., 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: 'page 2'
# This Image is directly from the websource
# By using AsyncImage you can use that
AsyncImage:
source: 'http://kivy.org / logos / kivy-logo-black-64.png'
##########################################################################
# Creating Page 3
# Using The Second Layout
# Creating GridLayout
GridLayout:
canvas:
Color:
rgba: 37 / 255., 39 / 255., 30 / 255., 1
Rectangle:
pos: self.pos
size: self.size
# Adding grids to Page 3
# It may be row or column
cols: 2
# In first Grid
# Adding Label + Image
Label:
text: 'page 3'
AsyncImage:
source: 'http://kivy.org/slides/kivyandroid-thumb.jpg'
# In Second Grid
# Adding Button + Image
Button:
text: 'Its User:):)'
on_press: print("Heloo User This is the Last Page")
AsyncImage:
source: 'http://kivy.org/slides/kivypictures-thumb.jpg'
# In third grid
# Adding Widget + Image
Widget
AsyncImage:
source: 'http://kivy.org/slides/particlepanda-thumb.jpg'
在这个文件中,我们正在创建/使用所有布局,因为在 .kv 文件中构建/使用所有这些更简单
.kv 文件的代码
Python3
# Program of How to use Multiple Layouts in Single .kv file
########################################################################
# creating page Layout
:
#########################################################################
# Creating Page 1
# Using BoxLayout inside PageLayout
BoxLayout:
# creating Canvas
canvas:
Color:
rgba: 216 / 255., 195 / 255., 88 / 255., 1
Rectangle:
pos: self.pos
size: self.size
# Providing orientation to the BoxLayout
orientation: 'vertical'
# Adding Label to Page 1
Label:
size_hint_y: None
height: 1.5 * self.texture_size[1]
text: 'page 1'
# Creating Button
Button:
text: 'GFG :)'
# Adding On_press function
# i.e binding function to press / touch
on_press: print("This Is The First Page")
#########################################################################
# Creating Page 2
BoxLayout:
orientation: 'vertical'
canvas:
Color:
rgba: 109 / 255., 8 / 255., 57 / 255., 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: 'page 2'
# This Image is directly from the websource
# By using AsyncImage you can use that
AsyncImage:
source: 'http://kivy.org / logos / kivy-logo-black-64.png'
##########################################################################
# Creating Page 3
# Using The Second Layout
# Creating GridLayout
GridLayout:
canvas:
Color:
rgba: 37 / 255., 39 / 255., 30 / 255., 1
Rectangle:
pos: self.pos
size: self.size
# Adding grids to Page 3
# It may be row or column
cols: 2
# In first Grid
# Adding Label + Image
Label:
text: 'page 3'
AsyncImage:
source: 'http://kivy.org/slides/kivyandroid-thumb.jpg'
# In Second Grid
# Adding Button + Image
Button:
text: 'Its User:):)'
on_press: print("Heloo User This is the Last Page")
AsyncImage:
source: 'http://kivy.org/slides/kivypictures-thumb.jpg'
# In third grid
# Adding Widget + Image
Widget
AsyncImage:
source: 'http://kivy.org/slides/particlepanda-thumb.jpg'
输出:
图 1:
图 2:
图 3:
视频输出:
参考: https://kivy.org/doc/stable/gettingstarted/layouts.html