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

Python使用.kv文件在Kivy中使用按钮

点击下载

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

按钮

按钮是带有相关操作的标签, 该操作在按下按钮时触发(或在单击/触摸后释放)。我们可以在按钮后面添加功能并设置按钮样式。

在本文中, 我们将讨论如何使用以下命令创建按钮.kv文件。我们还进行了一些按钮样式设计, 并且还定义了如何将按钮绑定到回调。

要使用按钮, 你必须导入:

import kivy.uix.button as Button
Basic Approach:

1) import kivy
2) import kivyApp
3) import Widget
4) import Button
5) Set minimum version(optional)
6) Create widget class:
          1) Arrange a callback
          2) Define Callback function
7) create App class
8) create .kv file (name same as the app class):
        1) create Widget
        2) Create Button
        3) Specify requirements
9) return Layout/widget/Class(according to requirement)
10) Run an instance of the class

常见问题之一是如何向按钮添加功能。所以要添加功能, 我们使用bind()function它将功能绑定到按钮。bind()创建一个发送到的事件打回来().

对于新的Kivy用户而言, 最常见的问题之一是误解bind方法的工作方式, 尤其是在尚未完全了解函数调用直觉的Python新用户中。

问题是bind方法不知道函数的存在或其参数, 它仅接收此函数调用的结果。就像给定代码中的那样, 当按下按钮时, 它将在函数回调中打印”按下按钮” def。

通过按钮动作和样式来实现上述方法的代码。

# import kivy module 
import kivy 
    
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require( "1.9.1" ) 
    
# base Class of your App inherits from the App class. 
# app:always refers to the instance of your application 
from kivy.app import App 
    
# creates the button in kivy 
# if not imported shows the error 
from kivy.uix.button import Button
  
# Widgets are elements of a graphical user 
# interface that form part of the User Experience. 
from kivy.uix.widget import Widget
  
  
# Creating a widget class 
# through this we add button 
# the commands of the class is in .kv file 
class Button_Widget(Widget):
  
     def __init__( self , * * kwargs):
  
         # Python super() function allows us to
         # refer to the parent class explicitly.
          
         super (Button_Widget, self ).__init__( * * kwargs)
  
         # creating Button    
         btn1 = Button(text = 'Hello World 1' , font_size = "15sp" , background_color = ( 1 , 1 , 1 , 1 ), color = ( 1 , 1 , 1 , 1 ), # size =(32, 32), # size_hint =(.2, .2), pos = ( 300 , 250 )) 
  
         # Arranging a callback to a button using
         # bind() function in kivy.
         btn1.bind(on_press = self .callback)
         self .add_widget(btn1)
  
     # callback function tells when button pressed
     # It tells the state and instance of button.
     def callback( self , instance):
         print ( "Button is pressed" )
         print ( 'The button % s state is <%s>' % (instance, instance.state))
  
# create App class 
class ButtonApp(App):
  
     def build( self ):
         # return the widget 
         return Button_Widget()
  
# run the App
if __name__ = = "__main__" :
     ButtonApp().run()

.kv文件的实现方法

# .kv file of the main.py code 
# Adding Button widget
  
<Button_Widget>:
  
     # defining Button size
     size: 100 , 100
  
     # creating Canvas 
     canvas.before:
         Color:
             rgba: 0.72 , 0.62 , 0.92 , 1
         Rectangle:
             pos: self .pos
             size: self .size

输出如下:

Python |使用.kv文件在Kivy中使用按钮1

显示按钮的操作图片:即单击按钮, 你将获得此输出

Python |使用.kv文件在Kivy中使用按钮2

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


赞(0)
未经允许不得转载:srcmini » Python使用.kv文件在Kivy中使用按钮

评论 抢沙发

评论前必须登录!