如果没有.kv文件,如何在kivy中进行此布局?
基本上我想做的就是在同一列
中安装Button2和button3from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
class TestApp(App):
def build(self):
root = FloatLayout()
layout = BoxLayout(orientation='vertical')
button1 = TextInput(size_hint=(1,.7))
button2 = TextInput(size_hint=(1,.2))
button3 = Button(text='Send',size_hint=(1, .1))
layout.add_widget(button1)
layout.add_widget(button2)
layout.add_widget(button3)
root.add_widget(layout)
return root
TestApp().run()
答案 0 :(得分:2)
将Button2和Button3添加到其他BoxLayout,然后将此BoxLayout添加为layout
的子项。
在build
方法中有类似的内容:
root = FloatLayout()
layout = BoxLayout(orientation='vertical')
layout1 = BoxLayout()
button1 = TextInput()
button2 = TextInput()
button3 = Button()
layout.add_widget(button1)
layout1.add_widget(button2)
layout1.add_widget(button3)
layout.add_widget(layout1)
root.add_widget(layout)
我假设这是你期望获得的行为:
答案 1 :(得分:0)
要构建图像中的内容,您有4个按钮和两个框布局,一个嵌套在另一个内。您不应该需要大小提示,因为2行的高度相同,底部3个按钮的宽度相同。上图中不需要浮动布局。
class TestApp(App):
def build(self):
# layout for the outer box layout to handle the rows
layout = BoxLayout(orientation='vertical')
# layout for the inner box layout, which is the bottom 3 columns
bottom_row = BoxLayout(size_hint_y=0.6) # Defaults to horizontal orientation
# Make the buttons
button1 = TextInput()
button2 = TextInput()
button3 = Button()
button4 = Button()
# Add the first button and then bottom row to the layout. Add the bottom 3 buttons to the bottom_row box layout.
layout.add_widget(button1)
bottom_row.add_widget(button2)
bottom_row.add_widget(button3)
bottom_row.add_widget(button4)
layout.add_widget(bottom_row)
return layout