KIVY:布局中的卡瓦尺寸

我正在从 “Kivy.Interactive Applications in Python “中学习KIVY,并试图按照下面给出的示例代码集成AnchorLayout、ScrollView和RelativeLayout。我正在学习 “Kivy: Interactive Applications in Python”,我试图按照下面给出的示例代码整合AnchorLayout、ScrollView和RelativeLayout。

在下面的代码中,我试图在ScrollView中可视化 “RelativeLayout”(在anchorlayout中anchor_x: ‘center’ and anchor_y: ‘center’ ),通过定义一个大小为self.size.Seems Rectangle被绘制在 “ToolBox “的顶部,它位于AnchorLayout: anchor_x: ‘center’ and anchor_y: ‘top’. 根据我的KV画布应该正好在 “ToolBox “和 “GeneralOptions “之间。但为什么会有这种重叠。

#File name: comiccreator.kv
<ComicCreator>:
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ToolBox:
            id : _tool_box
            size_hint: 1,None
            height: 20
            orientation: 'horizontal'
            Label:
                text: 'Tool Box'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'center'
        ScrollView:
            size_hint: 1,1
            RelativeLayout:
                size_hint: 1,None
                height: root.height - _tool_box.height - _general_options.height
                canvas.before:
                    Color: 
                        rgba: 1,0,0,0.5
                    Rectangle:
                        size: self.size
                        pos: self.pos
                DrawingSpace:
                    id: drawing_space
                    Label:
                        text: 'DrawingSpace'
                        pos : 0, 50
                        size: 50, 50
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        GeneralOptions:
            id: _general_options
            size_hint: 1,None
            height: 20
            orientation: 'horizontal'
            Label:
                text: 'General Options'

这个python脚本是…

# File name: comiccreator.py
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.core.window import Window

class DrawingSpace(RelativeLayout):
    pass

class ToolBox(BoxLayout):
    pass

class GeneralOptions(BoxLayout):
    pass

class ComicCreator(AnchorLayout):
    pass

class ComicCreatorApp(App):
    def build(self):
        return ComicCreator()

if __name__=="__main__":
    Window.size = (400, 200)
    ComicCreatorApp().run()

解决方案:

这里是你的一个版本 kv 不使用 AnchorLayouts:

#File name: comiccreator.kv
<ComicCreator>:
    ToolBox:
        id : _tool_box
        size_hint: 1,None
        height: 20
        orientation: 'horizontal'
        pos_hint: {'center_x':0.5, 'top':1}
        Label:
            text: 'Tool Box'
    ScrollView:
        size_hint: 1, None
        height: root.height - _tool_box.height - _general_options.height
        pos_hint: {'center_x':0.5}
        y: _general_options.height
        RelativeLayout:
            size_hint: 1,None
            height: root.height - _tool_box.height - _general_options.height
            canvas.before:
                Color: 
                    rgba: 1,0,0,0.5
                Rectangle:
                    size: self.size
                    pos: self.pos
            DrawingSpace:
                id: drawing_space
                Label:
                    text: 'DrawingSpace'
                    pos : 0, 50
                    size: 50, 50
    GeneralOptions:
        id: _general_options
        size_hint: 1,None
        height: 20
        pos_hint: {'center_x':0.5, 'y':0}
        orientation: 'horizontal'
        Label:
            text: 'General Options'

我已经改变了 ComicCreator 延长 RelativeLayout 而不是 AnchorLayout:

class ComicCreator(RelativeLayout):
    pass

给TA打赏
共{{data.count}}人
人已打赏
解决方案

需要用python编程逻辑解决一个问题[已关闭]。

2022-5-14 11:00:17

解决方案

获取对象或数组对象的值 php

2022-5-14 11:00:19

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索