为什么画在画布上的形状不会出现在一个单柱基维上?

2024-05-16 08:44:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在画布上画出无限多的形状,所以我决定用滚动视图来完成。但是,当我添加一个滚动视图时,画布上不会出现任何形状。你知道吗

副本:

# draws the shape
def draw_streak(self, obj):
    name = obj.id

    with open("streak.json", "r") as file:
        read = json.load(file)

    for key in read.keys():
        if key == name:
            with open("streak.json", "r+") as f:
                data = json.load(f)

            get_score = data.get(key, {}).get('score')

            can = self.root.get_screen("three")
            new_pos = can.pos
            for x in range(-1, get_score): # had to use -1 to get correct amount of shapes
                with can.ids.my_box.canvas:
                    Color(0, 1, 0, .75, mode='rgba')
                    rect = Rectangle(pos=new_pos, size=(30,30))
                new_pos[0] += rect.size[1]
                new_pos[0] += rect.size[0]

千伏:

<ScreenThree>
    id: screen_three
    name: "three"
    on_leave: my_box.canvas.clear()
    on_leave: selected_streak.canvas.clear()
    on_leave: del_space.canvas.clear()

    GridLayout:
        cols: 1
        rows: 2
        GridLayout:
            cols: 3
            rows: 1
            AnchorLayout:
                anchor_x: "left"
                anchor_y: "top"
                Button:
                    text: "Back"
                    size: 50, 25
                    size_hint: None, None
                    font_size: 18
                    on_release: app.root.current = "two"
            AnchorLayout:
                id: selected_streak
                anchor_x: "center"
                anchor_y: "top"
            AnchorLayout:
                id: del_space
                anchor_x: "right"
                anchor_y: "top"
        ScrollView:
            do_scroll_x: False
            do_scroll_y: True
            GridLayout:
                cols: 1
                id: my_box
                size_hint_y: None
                height: self.minimum_height
                row_force_default: True
                row_default_height: 50

另外,更改x大小提示是否会使不适合的形状在屏幕上向下移动?你知道吗

编辑

在特定时间按下按钮时称为“绘制条纹”。你知道吗

...
                elif delay > time.time() > self.honey:  # on time (green)
                    child.background_normal = ''
                    child.background_color = [0, 1, 0, .95]
                    child.unbind(on_press=self.early_click)
                    child.bind(on_press=self.add_score)
                    child.bind(on_press=self.display_streak)
                    child.bind(on_press=self.draw_streak)
                    child.unbind(on_press=self.late_click)

json值的键score与按钮id同名

编辑

截图

PageTwo

PageThree


Tags: posselfidjsonchildnewsizeget
1条回答
网友
1楼 · 发布于 2024-05-16 08:44:02

问题3

is there a way for my rectangles to move down once they exit the screen, or once they reach a certain number?

解决方案

从左到右和从上到下(lr-tb)方向绘制矩形。你知道吗

片段

    root = App.get_running_app().root
    can = root.get_screen('three')
    new_pos = can.pos
    new_pos[1] = root.height - 60    # below the Back button

    for x in range(-1, get_score): # had to use -1 to get correct amount of shapes
        with can.canvas:
            Color(0, 1, 0, .75, mode='rgba')
            rect = Rectangle(pos=new_pos, size=(30,30))

        new_pos[0] += rect.size[0] * 2    # x co-ordinate
        if new_pos[0] >= (root.width - rect.size[0]):
            new_pos[0] = 0    # x co-ordinate
            new_pos[1] -= rect.size[0] * 2    # y co-ordinate

输出-演示

Demot

问题2

wanted the shape to be drawn on page three, not the button.

解决方案

  • with can.canvas:替换with can.ids.my_box.canvas:

片段

        can = App.get_running_app().root.get_screen('three')
        new_pos = can.pos
        for x in range(-1, get_score): # had to use -1 to get correct amount of shapes
            with can.canvas:
                Color(0, 1, 0, .75, mode='rgba')
                rect = Rectangle(pos=new_pos, size=(30,30))
            new_pos[0] += rect.size[1]
            new_pos[0] += rect.size[0]

输出

Result - draw shapes on ScreenThree

在ScrollView中的按钮上绘制形状

  • 实现方法get_cell()以获取my_box内按钮的实例

片段

    def get_cell(self, key):
        obj = App.get_running_app().root.get_screen('three')

        for row in reversed(obj.ids.my_box.children):
            if row.children:    # children is not empty
                for child in reversed(row.children):
                    if isinstance(child, Button):
                        if child.id == key:
                            return child
            elif isinstance(row, Button):
                if row.id == key:
                    return row
        return None

    def draw_streak(self, obj):
       ... 
            button = self.get_cell(key)    # get button instance
            if button:    # Not None
                new_pos = button.pos    # get button's position

                for x in range(-1, get_score):
                    with button.canvas:
                        Color(0, 1, 0, .75, mode='rgba')
                        rect = Rectangle(pos=new_pos, size=(30,30))
                    new_pos[1] += rect.size[1]    # y co-ordinate
                    new_pos[0] += rect.size[0]    # x co-ordinate

输出

Demo

相关问题 更多 >