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

Python Kivy中的下拉列表用法示例

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

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

下拉列表

下拉列表可与自定义窗口小部件一起使用。它允许你在显示的小部件下显示小部件列表。与其他工具包不同, 小部件列表可以包含任何类型的小部件:简单的按钮, 图像等。

下拉列表的定位是全自动的:我们将始终尝试以用户可以在列表中选择项目的方式放置下拉列表。

制作下拉列表时要记住的一些重要事项:

添加小部件时, 我们需要手动指定高度(禁用size_hint_y), 以便下拉菜单可以计算所需的面积。

下拉列表中的所有按钮将触发下拉DropDown.select()方法。调用后, 主按钮文本将显示下拉菜单的选择。

要使用此小部件, 必须先导入:from kivy.uix.dropdown import DropDown

Basic Approach:
1) import kivy
2) import kivy App
3) import dropdown list
4) import button
5) set minimum version(optional)
6) import runTouchApp
7) Create dropdown
8) create runtouchApp method 
   which takes widget as an argument
   to run the App

实施方法–

# Program to explain how to creat drop-down in kivy 
     
# 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' ) 
     
# Importing Drop-down from the module to use in the program
from kivy.uix.dropdown import DropDown
  
# The Button is a Label with associated actions
# that are triggered when the button is pressed
# (or released after a click /touch)
from kivy.uix.button import Button
  
# another way used to run kivy app 
from kivy.base import runTouchApp
  
# create a dropdown with 10 buttons
dropdown = DropDown()
for index in range ( 10 ):
  
     # Adding button in drop down list
     btn = Button(text = 'Value % d' % index, size_hint_y = None , height = 40 )
  
     # binding the button to show the text when selected
     btn.bind(on_release = lambda btn: dropdown.select(btn.text))
  
     # then add the button inside the dropdown
     dropdown.add_widget(btn)
  
# create a big main button
mainbutton = Button(text = 'Hello' , size_hint = ( None , None ), pos = ( 350 , 300 ))
  
# show the dropdown menu when the main button is released
# note: all the bind() calls pass the instance of the caller 
# (here, the mainbutton instance) as the first argument of the callback
# (here, dropdown.open.).
mainbutton.bind(on_release = dropdown. open )
  
# one last thing, listen for the selection in the 
# dropdown list and assign the data to the button text.
dropdown.bind(on_select = lambda instance, x: setattr (mainbutton, 'text' , x))
  
# runtouchApp:
# If you pass only a widget in runtouchApp(), a Window will
# be created and your widget will be added to the window
# as the root widget.
runTouchApp(mainbutton)

输出如下:

图片1

Python Kivy中的下拉列表1

图片2:

Python Kivy中的下拉列表2

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


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

评论 抢沙发

评论前必须登录!