Python如何使用Kivy中的AnchorLayout?布局示例

2021年3月24日14:27:27 发表评论 1,118 次浏览

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

👉🏽Kivy教程–通过示例学习Kivy。

AnchorLayout:

AnchorLayout将其子项与边框(上, 下, 左, 右)或中心对齐。下面给出的类用于实现锚点布局

kivy.uix.anchorlayout.AnchorLayout

可以使用以下参数初始化AnchorLayout:

anchor_x
Parameters can be passed: "left", "right" and "center".

anchor_y
Parameters can be passed:"top", "bottom" and "center".

选择将小部件放置在父容器中的位置。

可以放置Anchorlayout来实现效果的9个不同的布局区域:左上, 顶部居中, 顶部右, 居中左, 居中居中, 居中右, 居左下, 居中居中和居右下。

注意:请记住, 将多个窗口小部件添加到锚点布局中, 仅将窗口小部件放置在同一位置。

Basic Approach:

1) import kivy
2) import kivyApp
4) import Anchorlayout
5) Set minimum version(optional)
6) create App class
7) return Layout/widget/Class(according to requirement)
8) Run an instance of the class

该方法的实施(带有一些样式):

1)anchor_x ="右", anchor_y ="下":

# Sample Python application demonstrating
# the working of AnchorLayout in Kivy
  
# Module imports
  
# base Class of your App inherits from the App class. 
# app:always refers to the instance of your application 
from kivy.app import App
  
# The AnchorLayout aligns its children to a border
# (top, bottom, left, right) or center
from kivy.uix.anchorlayout import AnchorLayout
  
# 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
  
# creates the button in kivy 
# if not imported shows the error
from kivy.uix.button import Button
  
  
# A Kivy app demonstrating the working of anchor layout
class AnchorLayoutApp(App):
      
     def build( self ):
  
          
         # Anchor Layout1
         layout = AnchorLayout(
         anchor_x = 'right' , anchor_y = 'bottom' )
         btn = Button(text = 'Hello World' , size_hint = (. 3 , . 3 ), background_color = ( 1.0 , 0.0 , 0.0 , 1.0 ))
      
         layout.add_widget(btn)
         return layout 
  
# creating the object root for AnchorLayoutApp() class  
root = AnchorLayoutApp()
# Run the Kivy app
root.run()

输出如下:

Python | Kivy中的AnchorLayout1

如果要更改位置锚布局然后只需将上面代码中的类代码替换为下面的代码, 或者你可以更改anchor_x和anchor_y与任何参数进行如上所述的9种组合。

2)anchor_x ="右", anchor_y ="顶部":

# A Kivy app demonstrating the working of anchor layout
class AnchorLayoutApp(App):
      
     def build( self ):
          
         # Anchor Layout1
         layout = AnchorLayout(
         anchor_x = 'right' , anchor_y = 'top' )
         btn = Button(text = 'Hello World' , size_hint = (. 3 , . 3 ), background_color = ( 1.0 , 0.0 , 1.0 , 1.0 ))
      
         layout.add_widget(btn)
         return layout

输出如下:

Python | Kivy中的AnchorLayout2

3)anchor_x ="中心", anchor_y ="顶部":

输出如下:

Python | Kivy中的AnchorLayout3

4)anchor_x ="左", anchor_y ="顶部":

输出如下:

Python | Kivy中的AnchorLayout4

5)anchor_x ="左", anchor_y ="底部":

输出如下:

Python | Kivy中的AnchorLayout5

6)anchor_x ="左", anchor_y ="中":

输出如下:

Python | Kivy中的AnchorLayout6

7)anchor_x ="中心", anchor_y ="中心":

输出如下:

Python | Kivy中的AnchorLayout7

8)anchor_x ="中心", anchor_y ="底部":

输出如下:

Python | Kivy中的AnchorLayout8

9)anchor_x ="右", anchor_y ="居中":

输出如下:

Python | Kivy中的AnchorLayout9

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


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: