【发布时间】:2021-05-07 09:35:41
【问题描述】:
我刚开始学习 Kivy,我试图了解 Canvas 指令如何影响 kivyApp 窗口大小的调整。
在 Kivy 文档中提到 -
Note
Kivy drawing instructions are not automatically relative to the position or size of the widget. You, therefore, need to consider these factors when drawing. In order to make your drawing instructions relative to the widget, the instructions need either to be declared in the KvLang or bound to pos and size changes.
example which follows 展示了如何将Rectangle 指令的大小和位置绑定到正在绘制它的小部件的大小和位置。因此,当调整窗口大小时,大小和位置会按比例变化。
但是,对于使用 points 的 Bezier 指令之类的指令,我该如何做同样的事情。
我有一个自定义小部件HangManFig1,它扩展了Widget 类,该类在KVlang 中定义,如下所示:
<HangManFig1>:
canvas:
Line:
points: (150, 100, 150, 700)
width: 10
Line:
points: (150, 700, 600, 700)
width: 10
Line:
points: (150, 600, 250, 700)
width: 10
Line:
points: (600, 700, 600, 600)
width: 3
Ellipse:
pos: (550, 500)
Line:
bezier: (610, 510, 630, 400, 570, 350)
width: 10
Line:
bezier: (570, 350, 510, 370, 450, 270)
width: 10
Line:
bezier: (570, 350, 600, 300, 550, 200)
width: 10
Line:
bezier: (610, 480, 530, 430, 500, 430)
width: 10
Line:
bezier: (610, 480, 630, 500, 680, 390)
width: 10
我在Screen 中使用这个小部件,方式如下:
#:import HangManFig1 figures.hangmanfig1
<MainScreen>:
name: '_main_screen_'
BoxLayout:
RelativeLayout:
size_hint_x: 0.7
HangManFig1:
RelativeLayout:
size_hint_x: 0.3
Button:
text: 'Play'
pos_hint: {'x': 0.1, 'y': 0.80}
size_hint: (0.8, 0.1)
on_release:
root.manager.transition.direction = 'left'
root.manager.current = '_game_screen_'
Button:
text: 'Practice'
pos_hint: {'x': 0.1, 'y': 0.60}
size_hint: (0.8, 0.1)
on_release:
root.manager.transition.direction = 'left'
root.manager.current = '_game_screen_'
Button:
text: 'Share'
pos_hint: {'x': 0.1, 'y': 0.40}
size_hint: (0.8, 0.1)
on_release:
root.manager.transition.direction = 'left'
root.manager.current = '_share_screen_'
Button:
text: 'Credits'
pos_hint: {'x': 0.1, 'y': 0.20}
size_hint: (0.8, 0.1)
on_release:
root.manager.transition.direction = 'left'
root.manager.current = '_credits_screen_'
当我调整窗口大小时,我看到虽然Buttons 的位置正确,但HangManFig1 却没有。
有没有办法可以将此小部件的大小绑定到父小部件的大小,以便即使窗口大小发生变化,它也能正确定位?
【问题讨论】:
标签: python kivy kivy-language