📜  Python – kivy 中的圆角按钮

📅  最后修改于: 2022-05-13 01:55:21.876000             🧑  作者: Mango

Python – kivy 中的圆角按钮

Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、Linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。

在本文中,我们将学习如何在 kivy Python中圆角按钮。

现在出现了一个问题——在 kivy 中为按钮创建圆角的首选方法是什么?

基本上,这是一个棘手的问题。我们知道小部件总是一个矩形,但我们可以改变小部件的背景,并分别使用按钮的一些属性,如 background_normal 和 background_down 属性,为按钮的正常和关闭状态放置几个图像。
此外,要使按钮的角变圆,您还必须了解按钮的另一个属性,即边框属性。

以上所有属性的语法:

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
     
##################################################    
# 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
 
# 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)
 
     
# class in which we are creating the imagebutton
class ButtonApp(App):
         
    def build(self):
 
        # create a fully styled functional button
        # Adding images normal.png and down.png
        btn = Button(text ="Push Me !",
                    background_normal = 'normal.png',
                    background_down = 'down.png',
                    border = (30, 30, 30, 30),                   
                    size_hint = (.3, .3),
                    pos_hint = {"x":0.35, "y":0.3}
                )
 
        # Returning the button
        return btn
             
     
# creating the object root for ButtonApp() class
root = ButtonApp()
     
# run function runs the whole program
# i.e run() method which calls the target
# function passed to the constructor.
root.run()


Basic Approach:

-> import kivy
-> import kivy App
-> import button
-> set minimum version(optional)
-> Extend the class :  
              -> create an image a button
              -> Do styling
              -> Use the border property to round the corners of the button
              -> Arrange call back if needed 
-> Add and return a button
-> Run an instance of the class

使用这两个名为 normal.png 和 down.png 的图像,您可以开始添加圆形边框。

我们将使用上面 2 个图像来创建圆形按钮。

下面是一段非常简单的代码,我们将尝试解释每一件事。

border 属性——border:30, 30, 30, 30 中的值告诉我们左、右、上、下有多少像素将用于按钮的边框。如果你给一个边框,例如边框:(150, 150, 150, 150),这将表现错误,原因是我们选择的边框比实际图像大。

现在下面是实现我的方法的代码:

Python3

## Sample Python application demonstrating that
## how to create button corners round in kivy
     
##################################################    
# 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
 
# 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)
 
     
# class in which we are creating the imagebutton
class ButtonApp(App):
         
    def build(self):
 
        # create a fully styled functional button
        # Adding images normal.png and down.png
        btn = Button(text ="Push Me !",
                    background_normal = 'normal.png',
                    background_down = 'down.png',
                    border = (30, 30, 30, 30),                   
                    size_hint = (.3, .3),
                    pos_hint = {"x":0.35, "y":0.3}
                )
 
        # Returning the button
        return btn
             
     
# creating the object root for ButtonApp() class
root = ButtonApp()
     
# run function runs the whole program
# i.e run() method which calls the target
# function passed to the constructor.
root.run()

输出:

注意:小部件仍然是矩形。这意味着即使您单击圆角,按钮仍会收到事件。