12

ボタン内で画像/アイコンとテキストを組み合わせるには、どのような方法が望ましいですか? たとえば、ボタンを作成し、text = 'my button'そのテキストの左側にグラフィック アイコンを表示するにはどうすればよいでしょうか。

4

3 に答える 3

19

質問#2について。

WidgetKivy が機能する方法は、インスタンスを埋め込むことです。Imageとは Widget のサブクラスであるためButton、ボタン内に Image を埋め込むだけで済みます。ウィジェット内の位置が固定されていることに注意してください。明示的な座標を指定する必要があります。

とは言っても、いつでも a を埋め込んでLayout、Button 内に入れているものを整理できます。

これが単純な例です

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を整理するために を使用しています。私が言ったように、Kivy はウィジェット内にウィジェットを埋め込んで動作します。ラベル、ボタン、レイアウトのいずれであっても問題ありません。ImageLabelButtonWidget

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()
于 2013-09-25T06:47:59.107 に答える