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

Python Kivy中的Accordion(手风琴)用法示例

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

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

Accordion:

“Accordion”小部件是菜单的一种形式, 其中选项垂直或水平堆叠, 并且焦点对准的项目(触摸时)打开以显示其内容。

它可以包含许多项目实例, 每个项目实例应包含一个根内容小部件。你最终将像一棵树。

当前实现将AccordionItem分为两部分:

  1. 标题栏的一个容器(由kv Templet制造)
  2. 一个容纳内容的容器

你可以增加标题栏的默认大小:

root =Accordion(min_space = 60)

或将方向更改为垂直:

root =Accordion(orientation =’vertical’)

AccordionItem更具可配置性, 你可以在折叠或打开该项目时设置自己的标题背景:

item = AccordionItem(background_normal =‘image_when_collapsed.png’, background_selected =’image_when_selected.png’)

Basic Approach:
1) import kivy
2) import kivyApp
3) import Accordian, AccordianItem
4) import Label
5) Create App class
6) return Layout/widget/Class(according to requirement)
7) Run an instance of the class

该方法的实施:

# How to use Accordion in kivy using .kv file 
   
# Program to Show how to create a switch 
# 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' ) 
   
# The Accordion widget is a form of menu
# where the options are stacked either vertically 
# or horizontally and the item in focus 
# (when touched) opens up to display its content.
from kivy.uix.accordion import Accordion, AccordionItem
  
# Label is the text which we want
# to add on our window, give to
# the buttons and so on
from kivy.uix.label import Label
  
  
# Create the App class
class AccordionApp(App):
      
     def build( self ):
         root = Accordion()
         root = Accordion(min_space = 60 )
         # Providing the orentation
         root = Accordion(orientation = 'vertical' )
  
         # Adding text to each Accordion
         for x in range ( 5 ):
             item = AccordionItem(title = 'Title % d' % x)
             item.add_widget(Label(text = 'GFG is Good Website foe CSE Students\n' * 5 ))
             root.add_widget(item)
  
         # Reurn the root
         return root
  
# Run the App
if __name__ = = '__main__' :
     AccordionApp().run()

输出如下:

Python Kivy中的Accordion(手风琴)用法示例1
Python Kivy中的Accordion(手风琴)用法示例2
Python Kivy中的Accordion(手风琴)用法示例3
Python Kivy中的Accordion(手风琴)用法示例4
Python Kivy中的Accordion(手风琴)用法示例5

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


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

评论 抢沙发

评论前必须登录!