📜  Python| kivy中的线(画布)

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

Python| kivy中的线(画布)

Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux、Windows等平台上运行,基本上是用来开发Android应用的,但并不代表不能在Desktops应用上使用。

线条画布:

Line 是一个顶点画布指令。它允许通过点绘制线。这段代码展示了如何使用/绘制圆形、椭圆、矩形和贝塞尔等扩展线图。

Basic Approach:

1) import kivy
2) import kivy App
3) import Gridlayout
4) import widget
5) set minimum version(optional)
6) Create as much as widget class as needed
7) create the App class
8) return the widget/layout etc class
9) Run an instance of the class

方法的实施:
# 主要.py

Python3
# kivy Lines Demo
 
# import kivy module 
import kivy 
     
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application   
from kivy.app import App 
     
 
# The GridLayout arranges children in a matrix.
# It takes the available space and
# divides it into columns and rows,
# then adds widgets to the resulting “cells”.
from kivy.uix.gridlayout import GridLayout
 
# Widgets are elements of
# a graphical user interface that
# form part of the User Experience.
from kivy.uix.widget import Widget
 
 
##############################################
# Classes form Different types of line as  widgets
 
class LineEllipse1(Widget):
    pass
 
class LineEllipse2(Widget):
    pass
 
class LineEllipse3(Widget):
    pass
 
class LineCircle1(Widget):
    pass
 
class LineCircle2(Widget):
    pass
 
class LineCircle3(Widget):
    pass
 
class LineCircle4(Widget):
    pass
 
class LineRectangle(Widget):
    pass
 
class LineBezier(Widget):
    pass
 
 
# Create the App class
class LineApp(App):
    def build(self):
 
        # Assign the number of column, spacing and padding
        root = GridLayout(cols = 3, padding = 50, spacing = 100)
 
        # Adding the widgets
        root.add_widget(LineEllipse1())
        root.add_widget(LineEllipse2())
        root.add_widget(LineEllipse3())
        root.add_widget(LineCircle1())
        root.add_widget(LineCircle2())
        root.add_widget(LineCircle3())
        root.add_widget(LineCircle4())
        root.add_widget(LineRectangle())
        root.add_widget(LineBezier())
        return root
 
# Run the App class
if __name__ == '__main__':
    LineApp().run()


Python3
# Line.kv file of the code
 
# Creating Different types of Lines(or shapes through line)
 
###########################################
 
# Row 1:
 
# Ellipse(1st row 1st element)
 
:
 
    # Creating Canvas
    canvas:
        Color:
            rgba: 1, .1, .1, .9
        # Ellipse Creation
        Line:
            width: 2.
            ellipse: (self.x, self.y, self.width, self.height)
 
    # Label the figure
    Label:
        center: root.center
        text: 'Ellipse'
 
 
############################################
 
# Ellipse from 90 to 180((1st row 2nd element))
 
:
    canvas:
        Color:
            rgba: 1, .1, .1, .9
        Line:
            width: 2.
            ellipse: (self.x, self.y, self.width, self.height, 90, 180)
    Label:
        center: root.center
        text: 'Ellipse from 90 to 180'
 
 
############################################
 
 
# Ellipse from 90 to 720, 10 segments(1st row 3rd element)
 
:
    canvas:
        Color:
            rgba: 1, .1, .1, .9
        Line:
            width: 2.
            ellipse: (self.x, self.y, self.width, self.height, 90, 720, 10)
    Label:
        center: root.center
        text: 'Ellipse from 90 to 720, 10 segments'
        halign: 'center'
 
############################################
 
# Circle(2nd row 1st element)
:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2)
    Label:
        center: root.center
        text: 'Circle'
 
############################################
 
# Circle from 90 to 180(2nd row 2nd element)
:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2, 90, 180)
    Label:
        center: root.center
        text: 'Circle from 90 to 180'
 
############################################
# Circle from 90 to 180, 10 segments(1st row 3rd element)
:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2, 90, 180, 10)
    Label:
        center: root.center
        text: 'Circle from 90 to 180, 10 segments'
        halign: 'center'
 
############################################
 
# Circle from 0 to 360 (3rd row 1st element)
:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2, 0, 360)
    Label:
        center: root.center
        text: 'Circle from 0 to 360'
        halign: 'center'
 
############################################
 
# Rectangle (3rd row 2nd element)
:
    canvas:
        Color:
            rgba: .1, .1, 1, .9
        Line:
            width: 2.
            rectangle: (self.x, self.y, self.width, self.height)
    Label:
        center: root.center
        text: 'Rectangle'
 
############################################
 
# Bezier (3rd row 3rd element)
:
    canvas:
        Color:
            rgba: .1, .1, 1, .9
        Line:
            width: 2.
            bezier:
                (self.x, self.y, self.center_x - 40, self.y + 100,
                self.center_x + 40, self.y - 100, self.right, self.y)
    Label:
        center: root.center
        text: 'Bezier'


Line.kv 文件:

Python3

# Line.kv file of the code
 
# Creating Different types of Lines(or shapes through line)
 
###########################################
 
# Row 1:
 
# Ellipse(1st row 1st element)
 
:
 
    # Creating Canvas
    canvas:
        Color:
            rgba: 1, .1, .1, .9
        # Ellipse Creation
        Line:
            width: 2.
            ellipse: (self.x, self.y, self.width, self.height)
 
    # Label the figure
    Label:
        center: root.center
        text: 'Ellipse'
 
 
############################################
 
# Ellipse from 90 to 180((1st row 2nd element))
 
:
    canvas:
        Color:
            rgba: 1, .1, .1, .9
        Line:
            width: 2.
            ellipse: (self.x, self.y, self.width, self.height, 90, 180)
    Label:
        center: root.center
        text: 'Ellipse from 90 to 180'
 
 
############################################
 
 
# Ellipse from 90 to 720, 10 segments(1st row 3rd element)
 
:
    canvas:
        Color:
            rgba: 1, .1, .1, .9
        Line:
            width: 2.
            ellipse: (self.x, self.y, self.width, self.height, 90, 720, 10)
    Label:
        center: root.center
        text: 'Ellipse from 90 to 720, 10 segments'
        halign: 'center'
 
############################################
 
# Circle(2nd row 1st element)
:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2)
    Label:
        center: root.center
        text: 'Circle'
 
############################################
 
# Circle from 90 to 180(2nd row 2nd element)
:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2, 90, 180)
    Label:
        center: root.center
        text: 'Circle from 90 to 180'
 
############################################
# Circle from 90 to 180, 10 segments(1st row 3rd element)
:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2, 90, 180, 10)
    Label:
        center: root.center
        text: 'Circle from 90 to 180, 10 segments'
        halign: 'center'
 
############################################
 
# Circle from 0 to 360 (3rd row 1st element)
:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2, 0, 360)
    Label:
        center: root.center
        text: 'Circle from 0 to 360'
        halign: 'center'
 
############################################
 
# Rectangle (3rd row 2nd element)
:
    canvas:
        Color:
            rgba: .1, .1, 1, .9
        Line:
            width: 2.
            rectangle: (self.x, self.y, self.width, self.height)
    Label:
        center: root.center
        text: 'Rectangle'
 
############################################
 
# Bezier (3rd row 3rd element)
:
    canvas:
        Color:
            rgba: .1, .1, 1, .9
        Line:
            width: 2.
            bezier:
                (self.x, self.y, self.center_x - 40, self.y + 100,
                self.center_x + 40, self.y - 100, self.right, self.y)
    Label:
        center: root.center
        text: 'Bezier'

输出: