在猕猴桃的按钮内合并图像和文本 [英] Combining image and text within a button in kivy
问题描述
在按钮内组合图像/图标和文本的首选方法是什么?例如,您将如何创建一个带有text = 'my button'
的按钮,并在该文本的左侧添加一个图形图标?
What's the preferred way to combine an image/icon and text within a button? For example, how would you create a button with text = 'my button'
, and a graphical icon to the left of that text?
推荐答案
关于问题2.
Kivy的工作方式是嵌入Widget
实例.由于Image
和Button
是Widget的子类,因此您要做的就是将Image嵌入Button中.请注意,小部件内的位置是固定的.您必须给出明确的坐标.
The way Kivy works is embedding Widget
instances. Since Image
and Button
are subclasses of Widget, then all you have to do is embed an Image inside a the Button. Notice that the positioning inside a widget is fixed. You have to give explicit coordinates.
也就是说,您始终可以嵌入Layout
来整理要放在Button中的内容.
That said, you can always embed a Layout
to organize the stuff you are putting inside the Button.
这是简单的ex
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_string("""
<ButtonsApp>:
orientation: "vertical"
Button:
text: "B1"
Image:
source: 'kivy.png'
y: self.parent.y + self.parent.height - 200
x: self.parent.x
Label:
text: "A label"
""")
class ButtonsApp(App, BoxLayout):
def build(self):
return self
if __name__ == "__main__":
ButtonsApp().run()
如何在按钮内嵌入相对布局的示例
在这种情况下,我正在使用StackLayout
来组织内部的Image
和Label
.如我所说,Button
是Widget
,而Kivy则将小部件嵌入小部件中.不管它们是标签,按钮还是布局.
In this case I am using a StackLayout
to organize an Image
and a Label
inside. As I said, Button
is a Widget
and Kivy works embedding widgets inside widgets. It doesn't matter if they are labels, buttons or layouts.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_string("""
<ButtonsApp>:
orientation: "vertical"
Button:
StackLayout:
pos: self.parent.pos
size: self.parent.size
orientation: 'lr-tb'
Image:
source: 'kivy.png'
size_hint_x: None
width: 74
Label:
size_hint_x: None
width: 100
text: "The text"
Label:
text: "A label"
""")
class ButtonsApp(App, BoxLayout):
def build(self):
return self
if __name__ == "__main__":
ButtonsApp().run()
这篇关于在猕猴桃的按钮内合并图像和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!