从 kivy,kv 文件中的按钮制作标签
make a label from a Button in kivy ,kv file
好的,看看谁能在 kivy .kv 文件中给出一个按钮的例子,它被设置为一个命令,当你按下它时,它会在按钮下方制作一个标签,
我试过这个
python
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.label import Label
Builder.load_file("my.kv")
class MyLayout(Widget,App):
def __init__(self, **kwargs):
super(MyLayout, self).__init__(**kwargs)
def addthelabel(self):
self.button = Label(text"you have just added me")
self.add_widget(self.button)
class UiApp(App):
def build(self):
return MyLayout()
UiApp().run()
.kv 文件
<MyLayout>:
BoxLayout:
orientation:"horizontal"
size: root.width, root.height
Button:
text:"hello"
on_press:
root.addthelabel()
但是当我 运行 它并点击按钮时,它不是我所期望的
图片
https://i.stack.imgur.com/ZoOCj.png
所以我需要一个新的例子,你们能帮忙吗?
您看到此行为的原因是因为在 addthelabel()
中,新 Label
被添加为根 MyWidget
布局的子项,而不是添加到 BoxLayout
包含现有按钮。
要获得您想要的行为,您需要将 id
添加到 kv
文件中的 BoxLayout
,这样您就可以从 Python 访问该小部件代码。您还需要将 orientation
更改为 vertical
。
<MyLayout>:
BoxLayout:
id: layout
orientation:"vertical"
size: root.width, root.height
Button:
text:"hello"
on_press: root.addthelabel()
然后在 Python 代码中,我们不想将新的 Label
添加到根小部件,而是要使用其新的 id
将其添加到 BoxLayout
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.label import Label
Builder.load_file("my.kv")
class MyLayout(Widget,App):
def __init__(self, **kwargs):
super(MyLayout, self).__init__(**kwargs)
def addthelabel(self):
self.button = Label(text="you have just added me")
self.ids.layout.add_widget(self.button)
class UiApp(App):
def build(self):
return MyLayout()
UiApp().run()
好的,看看谁能在 kivy .kv 文件中给出一个按钮的例子,它被设置为一个命令,当你按下它时,它会在按钮下方制作一个标签, 我试过这个
python
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.label import Label
Builder.load_file("my.kv")
class MyLayout(Widget,App):
def __init__(self, **kwargs):
super(MyLayout, self).__init__(**kwargs)
def addthelabel(self):
self.button = Label(text"you have just added me")
self.add_widget(self.button)
class UiApp(App):
def build(self):
return MyLayout()
UiApp().run()
.kv 文件
<MyLayout>:
BoxLayout:
orientation:"horizontal"
size: root.width, root.height
Button:
text:"hello"
on_press:
root.addthelabel()
但是当我 运行 它并点击按钮时,它不是我所期望的 图片 https://i.stack.imgur.com/ZoOCj.png 所以我需要一个新的例子,你们能帮忙吗?
您看到此行为的原因是因为在 addthelabel()
中,新 Label
被添加为根 MyWidget
布局的子项,而不是添加到 BoxLayout
包含现有按钮。
要获得您想要的行为,您需要将 id
添加到 kv
文件中的 BoxLayout
,这样您就可以从 Python 访问该小部件代码。您还需要将 orientation
更改为 vertical
。
<MyLayout>:
BoxLayout:
id: layout
orientation:"vertical"
size: root.width, root.height
Button:
text:"hello"
on_press: root.addthelabel()
然后在 Python 代码中,我们不想将新的 Label
添加到根小部件,而是要使用其新的 id
将其添加到 BoxLayout
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.label import Label
Builder.load_file("my.kv")
class MyLayout(Widget,App):
def __init__(self, **kwargs):
super(MyLayout, self).__init__(**kwargs)
def addthelabel(self):
self.button = Label(text="you have just added me")
self.ids.layout.add_widget(self.button)
class UiApp(App):
def build(self):
return MyLayout()
UiApp().run()