Kivy按钮不显示



根据我在youtube视频中看到的这段代码,有两个并排的按钮显示在kivy屏幕上,但在我的情况下有空白屏幕。没有按钮显示。提前感谢你的帮助。我是Python和groovy的新手。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget

class BoxLayoutExample(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
b1 = Button(text="Button A")
b2 = Button(text="Button B")
self.add_widget(b1)
self.add_widget(b2)

class MainWidget(Widget):
pass

class TheLabApp(App):
pass

TheLabApp().run()

应该可以;)问题是你刚刚在你的TheLabApp-class中键入了'pass'。当你使用。kv文件时,这可以工作,但你不是我所看到的。

class TheLabApp(App):
def build(self):
return BoxLayoutExample()

你应该看一下文档(https://kivy.org/doc/stable/api-kivy.app.html)来理解为什么你的代码不能工作。他们给了很好的例子。

编辑:

当你使用。kv文件时,你应该这样做:首先,将。kv文件命名为应用程序名称(TheLabApp.kv)

.kv

BoxLayoutExample:
<TheLabApp>:
BoxLayoutExample:

. py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget

class BoxLayoutExample(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
b1 = Button(text="Button A")
b2 = Button(text="Button B")
self.add_widget(b1)
self.add_widget(b2)

class MainWidget(Widget):
pass
class TheLabApp(App):
pass

TheLabApp().run()

最新更新