Python教程-Python Kivy库中的Spinner小部件
Kivy是Python中的图形用户界面工具,它是跨平台的。使用Kivy开发的应用程序可以在iOS、Windows、Linux和Android操作系统上使用。Kivy工具的基本用途是开发Android操作系统的应用程序,但也可以用于开发桌面应用程序。
Spinner小部件:
用户可以通过以下命令导入Kivy库的Spinner小部件:
from kivy.uix.spinner import Spinner
Spinner小部件用于从一组值中选择一个值。在默认状态下,Spinner显示其当前选择的值。当用户点击Spinner时,它会显示一个下拉菜单,显示用户可以选择的所有其他可用值。
与下拉框类似,Spinner小部件也用于为用户提供多个选择选项,以便从菜单中选择其中一个。用户还可以附加回调到Spinner小部件,以接收有关从小部件菜单中选择的值的通知。
实现步骤:
- 步骤 1: 导入Kivy库
- 步骤 2: 导入KivyApp
- 步骤 3: 导入Label
- 步骤 4: 导入Spinner
- 步骤 5: 导入FloatLayout
- 步骤 6: 设置最低Kivy版本(此步骤是可选的)
步骤 7: 创建一个App类:
- 首先,创建Spinner小部件
- 然后,将标签附加到Spinner小部件
- 然后,附加回调
- 步骤 8: 返回布局/小部件/类(根据需求)
- 步骤 9: 运行该类的实例。
示例:
from kivy.config import Config
# In this code:
# 0 means off
# 1 means ON
# Here, we can also use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
# Here, we are writing a program for Showing how to create a switch
# first, we will import the kivy module
import kivy
# the, base Class of our Application is inherited from the App class.
# app will refers to the instance of our application
from kivy.app import App as app1
# this will limit the kivy version that means
# below this kivy version we cannot
# use the application or software
kivy.require('1.9.0')
# The Label widget is for rendering text.
from kivy.uix.label import Label as lab
# Now, we will import the spinner widget
from kivy.uix.spinner import Spinner as spin
# This module consist the float layout
# for working with FloatLayout
# we have to import it first
from kivy.uix.floatlayout import FloatLayout as fl
# Now, we will create an Application by deriving from the App class
class Spinner_example(app1):
# now we will define build
def build_1(self):
# here, we will create floatlayout
layout = fl()
# then, we will create the spinner
# first, we will configure spinner object and then add it to the layout
self.spinnerObject = spin(text = "Option 1",
values = ("Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6"),
background_color =(0.784, 0.443, 0.216, 1))
self.spinnerObject.size_hint = (0.3, 0.2)
self.spinnerObject.pos_hint ={'x': .35, 'y':.75}
layout.add_widget(self.spinnerObject)
# return the layout
return layout;
# for, Running the application
if __name__ == '__main__':
Spinner_example().run()
输出:
图像 1:
图像 2:
现在,我们需要显示菜单列表中当前选择的选项。我们可以将标签显示在Spinner小部件旁边。
示例 2:
from kivy.config import Config
# In this code:
# 0 means off
# 1 means ON
# Here, we can also use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
# Here, we are writing a program for Showing how to create a switch
# first, we will import the kivy module
import kivy
# the, base Class of our Application is inherited from the App class.
# app will refers to the instance of our application
from kivy.app import App as app1
# this will limit the kivy version that means
# below this kivy version we cannot
# use the application or software
kivy.require('1.9.0')
# The Label widget is for rendering text.
from kivy.uix.label import Label as lab
# Now, we will import the spinner widget
from kivy.uix.spinner import Spinner as spin
# This module consist the float layout
# for working with FloatLayout
# we have to import it first
from kivy.uix.floatlayout import FloatLayout as fl
# Now, we will create an Application by deriving from the App class
class Spinner_example(app1):
# now we will define build
def build_1(self):
# here, we will create floatlayout
layout = fl()
# then, we will create the spinner
# first, we will configure spinner object and then add it to the layout
self.spinObject = spin(text = "Option 1",
values = ("Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6"),
background_color =(0.784, 0.443, 0.216, 1))
self.spinObject.size_hint = (0.3, 0.2)
self.spinObject.pos_hint ={'x': .35, 'y':.75}
layout.add_widget(self.spinObject)
self.spinObject.bind(text = self.on_spinner_select)
# It will change the label information as well
# It will add a label displaying the selection from the spinner
self.spinSelection = Label(text = "Selected value in spinner widegt is: %s"
%self.spinObject.text)
layout.add_widget(self.spinSelection)
self.spinSelection.pos_hint ={'x': .1, 'y':.3}
return layout;
# call back for the selection in spinner object
def on_spinner_select(self, spin, text):
self.spinSelection.text = ("Selected value in spinner widget is: %s" %self.spinObject.text)
print('The spinner widget', spin, 'have text', text)
# Run the app
if __name__ == '__main__':
Spinner_example().run()
输出:
图像 1:
图像 2:
结论
在本教程中,我们讨论了在Python应用程序中使用Kivy库的Spinner小部件来为用户提供从菜单中选择元素的选项。