2016-12-27 135 views
0

我的目標是讓按鈕完全充滿圖像。我的屏幕分成兩半。在右側,我想有九個按鈕,每個按鈕都完全填充不同的圖像,所有按鈕的尺寸相同。我想重塑這些圖像以適應按鈕,所以比例可能不得不改變。kivy圖像按鈕的大小和位置

這就是我的GUI現在的樣子。這些圖像不適合

enter image description here

我在KV文件試過幾次調整按鈕

,但現在我卡住了。

這是我的kv文件。

RadioRoot: 
<[email protected]>: 
    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() 

回答

0

在您的圖片標籤使用的是寬而不是大小...

嘗試:

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 :) 
+0

謝謝,工作完美。我也將keep_ratio設置爲False。 – PalimPalim