在 kivy 中使用 .kv 文件添加图像按钮
Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。
正如我们之前讨论过的如何使用图像,现在我们将学习如何使用图像并使用它们创建一个按钮。因此,在本文中,我们将学习如何使用.kv 文件功能将图像用作按钮,并为按钮提供一些样式。但在开始之前,让我们先了解一下按钮的一些属性——
background_down : Background image of the button used for the default graphical representation when the button is pressed.
background_normal : Background image of the button used for the default graphical representation when the button is not pressed.
background_disabled_normal : Background image of the button used for the default graphical representation when the button is disabled and not pressed.
These all 3 properties are a StringProperty that means they only takes string as values.
要使用按钮,您必须导入:
import kivy.uix.button as Button
Basic Approach:
1) import kivy
2) import kivyApp
3) import button
4) import FloatLayout
5) set minimum version(optional)
6) Create the Layout class
7) Create App class
8) Create .kv file:
1) Add Base class
2) Add Button properties
3) Add Image as button
4) Resizing, Positioning, functionality etc of Imagebutton
9) return instance of the layout class
10) Run an instance of the class
Kivy Tutorial – Learn Kivy with Examples.
本文中使用的图像 -
正常.png:
下来.png:
main.py 文件
Python3
## Sample Python application demonstrating that
## how to create button using image in kivy using .kv file
##################################################
# 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
# module consist the floatlayout
# to work with FloatLayout first
# you have to import it
from kivy.uix.floatlayout import FloatLayout
# creates the button in kivy
# if not imported shows the error
from kivy.uix.button import Button
# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
kivy.require('1.9.0')
# to change the kivy default settings we use this module config
from kivy.config import Config
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
# creating the root widget used in .kv file
class Base(FloatLayout):
# Adding functionality and arranging a callback to a button
def __init__(self, **kwargs):
super(Base, self).__init__(**kwargs)
def say_hello(self):
print("hello")
# class in which we are creating the imagebutton
# in .kv file to be named Btn.kv
class BtnApp(App):
# defining build()
def build(self):
# returning the instance of root class
return Base()
# run function runs the whole program
# i.e run() method which calls the target
# function passed to the constructor.
if __name__ == "__main__":
BtnApp().run()
Python3
#.kv file implementation of setting position, size and functionality of btn
# create a fully styled functional button
# Adding images normal.png and down.png
:
Button:
text: 'Hit me !!'
background_normal: 'normal.png'
background_down: 'down.png'
size_hint: .3, .3
pos_hint: {"x":0.35, "y":0.3}
on_press: root.say_hello()
.kv 文件:
Python3
#.kv file implementation of setting position, size and functionality of btn
# create a fully styled functional button
# Adding images normal.png and down.png
:
Button:
text: 'Hit me !!'
background_normal: 'normal.png'
background_down: 'down.png'
size_hint: .3, .3
pos_hint: {"x":0.35, "y":0.3}
on_press: root.say_hello()
输出 :
未按下按钮时
按下按钮时