Kivy 庫的微調器小部件

Kivy 是 Python 中的圖形用戶界面工具,與平台無關。使用 Kivy 開發的應用可以在 IOS、Windows、Linux 和 Android 操作系統上使用。Kivy 工具在為 Android 操作系統開發應用時的基本用途,但它也可以用於開發桌面應用。

微調器小部件:

用戶可以使用以下命令導入 kivy 庫中的微調器小部件:


from kivy.uix.spinner import Spinner

微調器小部件用於從集合中選擇一個值。在默認狀態下,微調器顯示其當前選定的值。當用戶單擊微調器時,它會顯示一個下拉菜單,顯示用戶可以從中選擇的所有其他可用值。

像組合框一樣,微調器小部件也用於向用戶提供多項選擇選項,以選擇其菜單中的任何一項。用戶還可以向微調器小部件附加一個回調,用於接收關於從小部件菜單中選擇值的通知。

方法:

  • 步驟 1: 我們將導入 kivy
  • 步驟 2: 我們將導入 kivyApp
  • 步驟 3: 我們將導入標籤
  • 第 4 步:我們將導入微調器
  • 步驟 5: 我們將導入浮動布局
  • 第 6 步:我們將設置最低版本(此步驟可選)
  • 第 7 步:我們將創建一個 App 類:
    • 首先,我們將創建微調器
    • 然後,我們將標籤貼在旋轉器上
    • 然後,我們將附加一個回調
  • 第八步:我們將返回布局/小部件/類(根據需求)
  • 第 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:

現在,我們必須顯示菜單列表中當前選擇了哪個選項。我們可以在微調器小部件旁邊顯示標籤。

例 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 庫的微調器小部件,為用戶提供從菜單中選擇元素的選項。


原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/279912.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-20 15:05
下一篇 2024-12-20 15:05

相關推薦

發表回復

登錄後才能評論