Python 如何在kivy窗口中放置两个按钮?

Python 如何在kivy窗口中放置两个按钮?,python,kivy,Python,Kivy,我知道这个问题可能已经被问过了,但我是Kivy的初学者,所以我想请人解释一下如何在同一屏幕上放置两个按钮 问题是我试图返回按钮变量,它就工作了。但是,当我试图同时返回两个时,它会给我一个错误 这是我的密码: from kivy.app import App from kivy.uix.label import Label from kivy.uix.button import Button from functools import partial class App(App): d

我知道这个问题可能已经被问过了,但我是Kivy的初学者,所以我想请人解释一下如何在同一屏幕上放置两个按钮

问题是我试图
返回
按钮变量,它就工作了。但是,当我试图同时返回两个时,它会给我一个错误

这是我的密码:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from functools import partial


class App(App):
    def com1(self, instance, *args):
        label1 = Label(text = "Hi")
        return label1
    def com2(self, instance, *args):
        label = Label(text= "Bye")
    def build(self):
        button1 = Button(text = "Hi", size_hint = (0.25, 0.18), pos = (350, 100))
        button1.bind(on_press=partial(self.com1, button1))
        button2 = Button(text = "Bye", size_hint = (0.25, 0.18), pos = (350, 200))
        button2.bind(on_press=partial(self.com2, button2))
        return button1, button2
App().run()

构建方法必须返回一个小部件,因此根据您希望按钮的排列方式,您有几个选项,如BoxLayout、RelativeLayout、FloatLayout等。在本例中,为了简单起见,我将使用BoxLayout:

# ... 
from kivy.uix.boxlayout import BoxLayout
# ...
class App(App):
    # ...
    def build(self):
        button1 = Button(text="Hi", size_hint=(0.25, 0.18), pos=(350, 100))
        button1.bind(on_press=partial(self.com1, button1))
        button2 = Button(text="Bye", size_hint=(0.25, 0.18), pos=(350, 200))
        button2.bind(on_press=partial(self.com2, button2))
        boxlayout = BoxLayout()
        boxlayout.add_widget(button1)
        boxlayout.add_widget(button2)
        return boxlayout
# ...
#。。。
从kivy.uix.boxlayout导入boxlayout
# ...
类应用程序(应用程序):
# ...
def生成(自):
button1=按钮(text=“Hi”,size_hint=(0.25,0.18),pos=(350100))
button1.绑定(按=部分(self.com1,button1))
button2=按钮(text=“Bye”,大小提示=(0.25,0.18),位置=(350200))
按钮2.绑定(按=部分(self.com2,按钮2))
boxlayout=boxlayout()
boxlayout.add_小部件(按钮1)
boxlayout.add_小部件(按钮2)
返回框布局
#…