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

Python如何在Kivy中使用多个KV文件

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

在本文中, 我们将看到如何使用多个.kv文件在单个应用中。

这是Python程序, 它使用网格布局作为根小部件。除了主要的kv文件, 它还会加载box1.kv, box2.kv和box3.kv。还有2个应用程序变量。这些变量是从主kv文件引用的。

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

Basic Approach:

1) import kivy
2) import kivyApp
3) import Gridlayout
4) import Builder
5) Set minimum version(optional)
6) Create Layout class
7) Create App class
8) Set up multiple .kv file
9) return Layout/widget/Class(according to requirement)
10) Run an instance of the class

main.py文件的实现:

# Multiple .kv file Python code
  
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
  
# 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
  
# Builder is a global Kivy instance used
# in widgets that you can use to load other
# kv files in addition to the default ones.
from kivy.lang import Builder
  
  
# Loading Multiple .kv files 
Builder.load_file( 'box1.kv' )
Builder.load_file( 'box2.kv' )
Builder.load_file( 'box3.kv' )
  
  
# Creating main kv file class
class main_kv(GridLayout):
     pass
  
# Create App class
class MainApp(App):
     def build( self ):
         self .x = 150
         self .y = 400
         return main_kv()
  
# run the App
if __name__ = = '__main__' :
     MainApp().run()

主kv文件包含一个包含3列的GridLayout。这3列包含不同的AnchorLayouts。这些都在主.kv文件。

现在是main.kv文件:

# Creating the main .kv files
# the difference is that it is
# the heart of the Application
# Other are just Organs
  
<main_kv>:
  
     # Assigning Grids
     cols: 3
  
     # Creating AnchorLayout
     AnchorLayout:
         anchor_x: 'left'
         anchor_y: 'center'
  
         # Canvas creation
         canvas:
             Color:
                 rgb: [ 1 , 0 , 0 ]
             Rectangle:
                 pos: self .pos
                 size: self .size
          
         Box1:
             size_hint: [ None , None ]
             size: [app.x, app.y]
  
     AnchorLayout:
         anchor_x: 'center'
         anchor_y: 'center'
         canvas:
             Color:
                 rgb: [ 0 , 1 , 0 ]
             Rectangle:
                 pos: self .pos
                 size: self .size
         Box2:
             size_hint: [ None , None ]
             size: [app.x, app.y]
  
     AnchorLayout:
         anchor_x: 'right'
         anchor_y: 'center'
         canvas:
             Color:
                 rgb: [ 0 , 0 , 1 ]
             Rectangle:
                 pos: self .pos
                 size: self .size
         Box3:
             size_hint: [ None , None ]
             size: [app.x, app.y]

现在, 如输出中所示, 每个网格中都有不同的按钮来创建按钮

在每个网格中, 我们都使用不同的.kv文件。

box1.kv文件–

# Creating 1st .kv file  
  
<Box1@BoxLayout>:
     Button:
         text: 'B1a'
     Button:
         text: 'B1b'

box2.kv文件–

# Creating 2nd .kv file
  
<Box2@BoxLayout>:
     Button:
         text: 'B2a'
     Button:
         text: 'B2b'

box3.kv文件–

# Creating 3rd .kv file 
  
<Box3@BoxLayout>:
     Button:
         text: 'B3a'
     Button:
         text: 'B3b'

输出:

Python如何在Kivy中使用多个KV文件1

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


赞(0)
未经允许不得转载:srcmini » Python如何在Kivy中使用多个KV文件

评论 抢沙发

评论前必须登录!