在kivy中,制作每行可变列数的屏幕的首选方法是什么?有没有办法在没有明确指定布局中窗口小部件的位置和大小的情况下完成此操作(即,是否有一种方法可以像在屏幕中堆叠一堆具有不同行数和列数的GridLayout一样)?使用python代码的方法是什么?
例如,假设您有一个包含某种类型布局的Screen,名为“layout_scr1”。你会如何安排事情,例如,layout_scr1的第一行包含1列,第二行包含2列,第三行包含4列?谢谢。
答案 0 :(得分:3)
有很多选项,但我认为最简单的方法是使用BoxLayout
代替GridLayout
甚至StackLayout
。 StackLayout
可能会转到第二行,宽度不够,而BoxLayout
和GridLayout
会停留在同一行。您可以找到并解释BoxLayout
和GridLayout
here之间的区别。
这是输出:
以下是代码:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
Builder.load_string("""
<Boxes>:
AnchorLayout:
anchor_x: 'center'
anchor_y: 'top'
ScreenManager:
size_hint: 1, .9
id: _screen_manager
Screen:
name: 'screen1'
BoxLayout:
orientation: 'vertical'
padding: 50
BoxLayout:
orientation: 'horizontal'
Button:
text: "1"
BoxLayout:
orientation: 'horizontal'
Button:
text: "2"
Button:
text: "3"
Button:
text: "4"
BoxLayout:
orientation: 'horizontal'
Button:
text: "5"
Button:
text: "6"
BoxLayout:
orientation: 'horizontal'
Button:
text: "7"
Button:
text: "8"
Button:
text: "9"
Button:
text: "10"
Screen:
name: 'screen2'
Label:
text: 'Another Screen'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'bottom'
BoxLayout:
orientation: 'horizontal'
size_hint: 1, .1
Button:
text: 'Go to Screen 1'
on_press: _screen_manager.current = 'screen1'
Button:
text: 'Go to Screen 2'
on_press: _screen_manager.current = 'screen2'""")
class Boxes(FloatLayout):
pass
class TestApp(App):
def build(self):
return Boxes()
if __name__ == '__main__':
TestApp().run()
如果您仍想使用GridLayouts
,可以替换:
BoxLayout:
orientation: 'vertical'
为此:
GridLayout:
cols: 1
和此:
BoxLayout:
orientation: 'vertical'
为此:
GridLayout:
cols: 1
以防万一你正在寻找一种更有活力的方法:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
Builder.load_string("""
<Boxes>:
boxes: _boxes
AnchorLayout:
anchor_x: 'center'
anchor_y: 'top'
ScreenManager:
size_hint: 1, .9
id: _screen_manager
Screen:
name: 'screen1'
BoxLayout:
orientation: 'vertical'
padding: 50
id: _boxes
Screen:
name: 'screen2'
Label:
text: 'Another Screen'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'bottom'
BoxLayout:
orientation: 'horizontal'
size_hint: 1, .1
Button:
text: 'Go to Screen 1'
on_press: _screen_manager.current = 'screen1'
Button:
text: 'Go to Screen 2'
on_press: _screen_manager.current = 'screen2'""")
class Boxes(FloatLayout):
def __init__(self, **kwargs):
super(Boxes, self).__init__(**kwargs)
bx1 = BoxLayout(orientation='horizontal')
bx2 = BoxLayout(orientation='horizontal')
bx3 = BoxLayout(orientation='horizontal')
bx4 = BoxLayout(orientation='horizontal')
for i in range(1,2):
bx1.add_widget(Button(text=str(i)))
for i in range(2,5):
bx2.add_widget(Button(text=str(i)))
for i in range(5,7):
bx3.add_widget(Button(text=str(i)))
for i in range(7,11):
bx4.add_widget(Button(text=str(i)))
self.boxes.add_widget(bx1)
self.boxes.add_widget(bx2)
self.boxes.add_widget(bx3)
self.boxes.add_widget(bx4)
class TestApp(App):
def build(self):
return Boxes()
if __name__ == '__main__':
TestApp().run()