簡體   English   中英

為什么 kivy 沒有畫線

[英]Why is kivy isn't drawing lines

我試圖制作 Galaxy 項目。 我在兩者之間犯了一個錯誤!

這個我是通過freecodecamp.org kivy 全程嘗試的。

我是 kivy 的初學者。

查看canvas是否有問題。

我無法在 kivy window 上看到我畫的線。

這是我的文件:

主程序

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import *
from kivy.lang import Builder
from kivy.properties import NumericProperty

Builder.load_file('galaxy.kv')
class MainWidget(Widget):
    perspective_point_x = NumericProperty(0)
    perspective_point_y = NumericProperty(0)

    V_NB_LINES = 7
    V_NB_SPACING = .1
    vertical_lines = []

    def __init__(self, **kwargs):
        super(MainWidget, self).__init__(**kwargs)
        self.init_vertical_lines()

    def on_parent(self, widget, parent):
        pass

    def on_size(self, *args):
        self.update_vertical_lines()

    def on_perspective_point_x(self, widget, value):
        pass

    def on_perspective_point_y(self, widget, value):
        pass

    def init_vertical_lines(self):
        with self.canvas:
            Color(1, 1, 1)
            for i in range(0, self.V_NB_LINES):
                self.vertical_lines.append(Line())

    def update_vertical_lines(self):
        central_line_x = int(self.width/2)
        spacing = int(self.V_NB_SPACING * self.width)
        offset = -int(self.V_NB_LINES/2)
        for i in range(0, self.V_NB_LINES):
            line_x = central_line_x + offset*spacing
            self.vertical_lines[i].points = [line_x, 0, line_x, self.height]
            offset += 1

class GalaxyApp(App):
    pass

GalaxyApp().run()

星系.kv

<MainWidget>
    perspective_point_x: self.width  / 2
    perspective_point_y: self.height * 0.75

您在 GalaxyApp 中缺少構建 function

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import *
from kivy.lang import Builder
from kivy.properties import NumericProperty

Builder.load_file('galaxy.kv')

class MainWidget(Widget):
    perspective_point_x = NumericProperty(0)
    perspective_point_y = NumericProperty(0)

    V_NB_LINES = 7
    V_NB_SPACING = .1
    vertical_lines = []

    def __init__(self, **kwargs):
        super(MainWidget, self).__init__(**kwargs)
        self.init_vertical_lines()

    def on_parent(self, widget, parent):
        pass

    def on_size(self, *args):
        self.update_vertical_lines()

    def on_perspective_point_x(self, widget, value):
        pass

    def on_perspective_point_y(self, widget, value):
        pass

    def init_vertical_lines(self):
        with self.canvas:
            Color(1, 1, 1)
            for i in range(0, self.V_NB_LINES):
                self.vertical_lines.append(Line())

    def update_vertical_lines(self):
        central_line_x = int(self.width/2)
        spacing = int(self.V_NB_SPACING * self.width)
        offset = -int(self.V_NB_LINES/2)
        for i in range(0, self.V_NB_LINES):
            line_x = central_line_x + offset*spacing
            with self.canvas:
                self.vertical_lines[i].points = [line_x, 0, line_x, self.height]
            offset += 1

class GalaxyApp(App):
    def build(self):
        return MainWidget()

GalaxyApp().run()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM