Python使用.kv文件的kivy下拉列表示例

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

????Kivy教程–通过示例学习Kivy。
下拉列表
下拉列表可与自定义窗口小部件一起使用。它允许你在显示的小部件下显示小部件列表。与其他工具包不同, 小部件列表可以包含任何类型的小部件:简单的按钮, 图像等。
下拉列表的定位是全自动的:我们将始终尝试以用户可以在列表中选择项目的方式放置下拉列表。
要使用此小部件, 必须先导入:from kivy.uix.dropdown import DropDown
Basic Approach: 1) import kivy 2) import kivyApp 3) import dropdown 4) import Floatlayout(according to need) 5) Set minimum version(optional) 6) Create Layout class 7) Create App class 9) create .kv file (name same as the app class): 1) create Dropdown 2) create callback 3) And many more styling as needed 10) return Layout/widget/Class(according to requirement) 11) Run an instance of the class

#.py文件
''' Code of How to use drop-down list with.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' ) # drop-down menu is a list of items that # appear whenever a piece of text or a # button is clicked. # To use drop down you must have ti import it from kivy.uix.dropdownimport DropDown# module consists the floatlayout # to work with FloatLayout first # you have to import it from kivy.uix.floatlayout import FloatLayout# 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 . buttonimport Buttonclass CustomDropDown(DropDown): passclass DropdownDemo(FloatLayout): '''The code of the application itself.''' def __init__( self , * * kwargs):'''The button at the opening of the window is created here, not in kv ''' super (DropdownDemo, self ).__init__( * * kwargs) self .dropdown = CustomDropDown()# Creating a self widget bouton self .mainbutton = Button(text = 'Do you in college?' , size_hint_x = 0.6 , size_hint_y = 0.15 )# Added button to FloatLayout so inherits this class self .add_widget( self .mainbutton)# Adding actions # If click self .mainbutton.bind(on_release = self .dropdown. open )# root.select on_select called self .dropdown.bind(on_select = lambda \ instance, x: setattr ( self .mainbutton, 'text' , x)) self .dropdown.bind(on_select = self .callback)def callback( self , instance, x): '''x is self.mainbutton.text refreshed''' print ( "The chosen mode is: {0}" . format ( x ) )class MainApp(App): '''The build function returns root, here root = DropdownDemo (). root can only be called in the kv file. ''' def build( self ): return DropdownDemo()if __name__ = = '__main__' :MainApp().run()

【Python使用.kv文件的kivy下拉列表示例】.kv文件:
< CustomDropDown> : Button: text: 'College Name' size_hint_y: None height: 44 on_release: root.select( 'College is' ) Label: text: 'Not in college' size_hint_y: None height: 44 Button: text: 'KccItm' size_hint_y: None height: 44 on_release: root.select( 'Kcc' )

输出如下:
Python使用.kv文件的kivy下拉列表示例

文章图片
Python使用.kv文件的kivy下拉列表示例

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

    推荐阅读