Python – 使用 .kv 文件在 kivy 中圆角按钮
Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。
在本文中,我们将学习如何使用Python中的 .kv 文件在 kivy 中圆角按钮。
小部件总是矩形的,但我们可以通过分别使用按钮的一些属性,如 background_normal 和 background_down 属性来更改小部件的背景并为按钮的正常和按下状态放置几个图像。此外,要使按钮的角变圆,您还必须了解按钮的另一个属性,即边框属性。
background_down :
1) Background image of the button used for the default graphical representation when the button is pressed.
2) background_down is a StringProperty .
background_normal :
1) Background image of the button used for the default graphical representation when the button is not pressed.
2) background_normal is also a StringProperty
border :
1) Border used for BorderImage graphics instruction. Used with background_normal and background_down. Can be used for custom backgrounds.
2) It must be a list of four values: (bottom, right, top, left).
3) border is a ListProperty and defaults to (16, 16, 16, 16)
以上所有属性的语法:
Python3
background_normal: 'normal.png'
background_down: 'down.png'
border: 30, 30, 30, 30
Python3
## Sample Python application demonstrating that
## how to create button corners round 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):
pass
# 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 rounding the corners of button
# create a fully styled functional button
# Adding images normal.png and down.png
:
Button:
text: 'Hit me !!'
# Button properties for image
background_normal: 'normal.png'
background_down: 'down.png'
# To round the corners we use border
border: 30, 30, 30, 30
# Adding background color to button you can comment it
background_color: 0.1, 0.5, 0.6, 1
size_hint: .3, .3
pos_hint: {"x":0.35, "y":0.3}
Basic Approach:
1) import kivy
2) import kivyApp
3) import button and floatlayout
4) set minimum version(optional)
5) Create the Layout class
6) Create App class
7) Create .kv file:
1) Add Base class
2) Add Button properties
3) Add Image as button
4) Round the corners of the button using border property
8) return instance of the layout class
9) Run an instance of the class
Kivy Tutorial – Learn Kivy with Examples.
下面是我在这个ie中使用的两张图片:
normal.png 和 down.png
现在下面是实现我的方法的代码:
主文件
Python3
## Sample Python application demonstrating that
## how to create button corners round 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):
pass
# 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()
btn.kv
Python3
#.kv file implementation of rounding the corners of button
# create a fully styled functional button
# Adding images normal.png and down.png
:
Button:
text: 'Hit me !!'
# Button properties for image
background_normal: 'normal.png'
background_down: 'down.png'
# To round the corners we use border
border: 30, 30, 30, 30
# Adding background color to button you can comment it
background_color: 0.1, 0.5, 0.6, 1
size_hint: .3, .3
pos_hint: {"x":0.35, "y":0.3}
输出: