在 kivy 中使用画布的圆形(类似椭圆形)按钮(使用 .kv 文件)
Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。
在本文中,我们将学习如何使用画布在 kivy 中创建圆形或圆形按钮。您必须对画布、按钮及其属性有一个非常清晰的概念,才能了解如何制作这样的按钮。众所周知,canvas 是 Widget 用于绘制的根对象。 Kivy 中的每个 Widget 默认已经有一个 Canvas。创建小部件时,您可以创建绘图所需的所有指令。
要使用画布,您必须在文件中导入图形。
from kivy.graphics import Rectangle, Color
Some important properties used in this article –
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)
Button behavior :
1) The Button Behavior mix in class provides Button behavior.
2) You can combine this class with other widgets, such as an Image, to provide alternative buttons that preserve Kivy button behavior.
Basic Approach -
-> import kivy
-> import kivy App
-> import widget
-> import Canvas i.e.:
from kivy.graphics import Rectangle, Color
-> set minimum version(optional)
-> Extend the Widget class
-> Create the App Class
-> create the .kv file:
-> create the button using the canvas
-> Use border property to give them a circular shape.
-> Add action/callback if needed
-> return a Widget
-> Run an instance of the class
Kivy Tutorial – Learn Kivy with Examples
方法的实施
主文件
Python3
## Sample Python application demonstrating the
## working of canvas with buttonbehaviour i.e
## creating a circular button 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
# From graphics module we are importing
# Rectangle and Color as they are
# basic building of canvas.
from kivy.graphics import Rectangle, Color
# The ButtonBehavior mixin class provides Button behavior.
from kivy.uix.button import ButtonBehavior
# The Label widget is for rendering text.
from kivy.uix.label import Label
# class in which we are creating the canvas
class CircularButton(ButtonBehavior, Label):
pass
# Create the App Class
class BtnApp(App):
def build(self):
return CircularButton()
# run the App
BtnApp().run()
Python3
# .kv file of creating a circular button using canvas
:
# Creating Circular button
canvas:
# Color is different if button is pressed
Color:
rgb: (0, 1, 0, 1) if self.state == 'normal' else (1, 0, 1, 1)
# Rounded rectangle canvas
RoundedRectangle:
# Giving the size and the position
size: (self.size)
pos: (self.pos)
# This will force the rectangle to be the circle
radius: [400, ]
# Print the text when touched or button pressed
on_release:
print("This is the button made up by the canvas")
.kv 文件
Python3
# .kv file of creating a circular button using canvas
:
# Creating Circular button
canvas:
# Color is different if button is pressed
Color:
rgb: (0, 1, 0, 1) if self.state == 'normal' else (1, 0, 1, 1)
# Rounded rectangle canvas
RoundedRectangle:
# Giving the size and the position
size: (self.size)
pos: (self.pos)
# This will force the rectangle to be the circle
radius: [400, ]
# Print the text when touched or button pressed
on_release:
print("This is the button made up by the canvas")
输出:
注意:小部件仍然是矩形。这意味着即使您单击圆角,按钮仍会收到事件。