在多个按钮上显示图像kivy

2024-05-01 21:25:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在多个不同的按钮上显示一个图像(如图:https://i.stack.imgur.com/BQ99R.jpg

当我尝试这样做时,图像会被按钮覆盖并隐藏在背景中。在

我想知道的是这在基维是可能的吗?如果是,我在哪里定义图像,在标签或按钮中?在


Tags: https图像com定义stack标签按钮jpg
1条回答
网友
1楼 · 发布于 2024-05-01 21:25:01

此代码:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder


Builder.load_string("""
<Base>:
    GridLayout:
        cols: 2
        size_hint: 1, 1
        Button:
            text: "Button 1"
            on_press: print(self.text)
        Button:
            text: "Button 2"
            on_press: print(self.text)
        Button:
            text: "Button 3"
            on_press: print(self.text)
        Button:
            text: "Button 4"
            on_press: print(self.text)
    Image:
        source: 'wolf.png'
""")


class Base(FloatLayout):
    def __init__(self, **kwargs):
        super(Base, self).__init__(**kwargs)


class SampleApp(App):
    def build(self):
        return Base()


if __name__ == "__main__":
    SampleApp().run()

产生这个: enter image description here

当然,你必须自己提供一个形象;o)

相关问题 更多 >