📌  相关文章
📜  Python|使用 .kv 文件在 Kivy 中画布

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

Python|使用 .kv 文件在 Kivy 中画布

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

帆布 :

Canvas 是 Widget 用于绘制的根对象。 kivy 画布不是您绘画的地方。
Kivy 中的每个 Widget 默认已经有一个 Canvas。创建小部件时,您可以创建绘图所需的所有指令。如果 self 是您当前的小部件。颜色和矩形指令会自动添加到画布对象中,并在绘制窗口时使用。

要使用 Canvas,您必须导入:

from kivy.graphics import Rectangle, Color
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 canvas
    -> Add action/callback if needed
-> return a Widget
-> Run an instance of the class
方法的实施:

1)创建一个简单的画布:

main.py 文件

Python3
# 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 
  
# A Widget is the base building block
# of GUI interfaces in Kivy.
# It provides a Canvas that
# can be used to draw on screen.
from kivy.uix.widget import Widget
  
# From graphics module we are importing
# Rectangle and Color as they are
# basic building of canvas.
from kivy.graphics import Rectangle, Color
  
  
# class in which we are creating the canvas
class CanvasWidget(Widget):
    pass
  
# Create the App Class
class CanvasApp(App):
    def build(self):
        return CanvasWidget()
  
# run the App
CanvasApp().run()


Python3
# .kv file of canvas
  

  
    # creating canvas
    canvas:
        Color:
            rgba: 0, 0, 1, 1    # Blue
  
        # size and position of Canvas
        Rectangle:
            pos: self.pos
            size: self.size


Python3
# 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 CanvasWidget(ButtonBehavior, Label):
    pass
  
# Create the App Class
class CanvasApp(App):
    def build(self):
        return CanvasWidget()
  
# run the App
CanvasApp().run()


Python3
# .kv file of canvas
  

  
    # Creating Canvas
    canvas:
  
        # Color is blue if button is pressed,
        # otherwise color is red
        Color: 
            rgb: (1, 0, 0, 1) if self.state == 'normal' else (0, 0, 1, 1)
      
        # Rounded rectangle canvas
        RoundedRectangle:
            size: self.size
            pos: self.pos
              
            # Play with these if you want smooth corners for your button
            radius: 100, 100, 100, 100
          
  
    # Print the text when touched or button pressed    
    on_release:
        print("I have been clicked")


画布.kv 文件:

Python3

# .kv file of canvas
  

  
    # creating canvas
    canvas:
        Color:
            rgba: 0, 0, 1, 1    # Blue
  
        # size and position of Canvas
        Rectangle:
            pos: self.pos
            size: self.size

输出:

现在我们如何通过任何操作更改画布的颜色所以下面是通过单击屏幕颜色更改的示例。

main.py 文件:

Python3

# 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 CanvasWidget(ButtonBehavior, Label):
    pass
  
# Create the App Class
class CanvasApp(App):
    def build(self):
        return CanvasWidget()
  
# run the App
CanvasApp().run()

.kv 文件:

Python3

# .kv file of canvas
  

  
    # Creating Canvas
    canvas:
  
        # Color is blue if button is pressed,
        # otherwise color is red
        Color: 
            rgb: (1, 0, 0, 1) if self.state == 'normal' else (0, 0, 1, 1)
      
        # Rounded rectangle canvas
        RoundedRectangle:
            size: self.size
            pos: self.pos
              
            # Play with these if you want smooth corners for your button
            radius: 100, 100, 100, 100
          
  
    # Print the text when touched or button pressed    
    on_release:
        print("I have been clicked")

输出:

笔记:
Kivy 绘图指令不会自动相对于小部件的位置或大小。因此,您在绘图时需要考虑这些因素。为了使您的绘图指令相对于小部件,指令需要在 KvLang 中声明或绑定到 pos 和 size 更改。