【问题标题】:kivy image button sizekivy 图像按钮大小
【发布时间】:2016-12-27 23:38:23
【问题描述】:

我的目标是让按钮完全充满图像。我的屏幕被分成两半。在右侧,我希望有九个按钮,每个按钮都完全填充了不同的图像,所有按钮的尺寸都相同。我想重塑这些图像以适合按钮,所以比例可能必须改变。

这就是我的 GUI 现在的样子。图片不适合按钮

我在我的 kv 文件中尝试了一些调整,但现在我卡住了。

这是我的 kv 文件。

RadioRoot:
<RadioRoot@BoxLayout>:
    BoxLayout:
        BoxLayout:
            BoxLayout:
                orientation: "vertical"
                Label:
                    size_hint_y: 4
                    text: "info about radio"
                BoxLayout:
                    size_hint_y: 1
                    BoxLayout:
                        orientation: "vertical"
                        BoxLayout:
                            Button:
                                text: "Previous"
                                on_press: root.previous()
                            Button:
                                text: "Play/Stop"
                                on_press: root.play_stop()
                            Button:
                                text: "Next"
                                on_press: root.next()
                Button:
                    size_hint_y: 1
                    text: "Shutdown"
                    on_press: root.shutdown()

        BoxLayout:
            BoxLayout:
                orientation: "vertical"
                BoxLayout:


                    Button:
                        text: "Channel1"
                        on_press: root.channel(1)
                        #size_hint_y: None
                        #size_hint_x: None
                        Image:
                            source: 'swr3.png'
                            size_hint_y: None
                            size_hint_x: None
                            y: self.parent.y + .5* self.parent.height -.5 * self.parent.width/self.image_ratio
                            x: self.parent.x
                            #heigth: self.parent.width/self.image_ratio
                            #heigth: self.parent.height
                            width: self.parent.width
                            keep_ratio: True
                            allow_stretch: True
                    Button:
                        text: "Channel2"
                        on_press: root.channel(2)
                        Image:
                            source: 'flux.png'
                            width: self.parent.width
                            size_hint_y: None
                            size_hint_x: None
                            y: self.parent.y + .5* self.parent.height -.5 * self.parent.width/self.image_ratio
                            x: self.parent.x

                            keep_ratio: True
                            allow_stretch: True
                    Button:
                        text: "Channel3"
                        on_press: root.channel(3)
                BoxLayout:

                    Button:
                        text: "Channel4"
                        on_press: root.channel(4)
                    Button:
                        text: "Channel5"
                        on_press: root.channel(5)
                    Button:
                        text: "Channel6"
                        on_press: root.channel(6)
                BoxLayout:

                    Button:
                        text: "Channel7"
                        on_press: root.channel(7)
                    Button:
                        text: "Channel8"
                        on_press: root.channel(8)
                    Button:
                        text: "Channel9"
                        on_press: root.channel(9)

这是对应的python文件

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label


class PhilippsRadioApp(App):
    pass

class RadioRoot(BoxLayout):

    def previous(self):
        print("Previous")

    def play_stop(self):
        print("Play/Stop")


    def next(self):
        print("Next")

    def shutdown(self):
        print("Shutdown")

    def channel(self, num):
        print("Channel")


if __name__ == '__main__':
    PhilippsRadioApp().run()

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    在您的图像标签中,您使用的是宽度而不是大小...

    尝试:

    Image:
        ...
        size: self.parent.size #I think you can remove the size hints since they don't add anything...
        stretch: True #keep this one as well :)
    

    【讨论】:

    • 谢谢,完美运行。我还将 keep_ratio 设置为 False。
    猜你喜欢
    • 2021-04-04
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 2016-09-13
    • 2021-06-08
    相关资源
    最近更新 更多