2014-01-12 43 views
0

我想開發一個kaivy應用程序,我對此很新。我試圖用兩個按鈕做一個簡單的屏幕,但我看到只有一個按鈕(button1,檢入)。我想並排顯示兩個按鈕。任何幫助,高度讚賞。Kivy不顯示兩個按鈕

from kivy.app import App 
from kivy.lang import Builder 

button1 = ''' 
FloatLayout: 

    Button: 
     text: 'Check In' 
     size_hint: None, None 
     pos_hint: {'center_x': .5, 'center_y': .5} 
     canvas.before: 
      PushMatrix 
      Rotate: 
       angle: 0 
       origin: self.center 
     canvas.after: 
      PopMatrix 
''' 

button2 = ''' 
FloatLayout: 

    Button: 
     text: 'SOS' 
     size_hint: None, None 
     pos_hint: {'center_x': 1.5, 'center_y': 1.5} 
     canvas.before: 
      PushMatrix 
      Rotate: 
       angle: 45 
       origin: self.center 
     canvas.after: 
      PopMatrix 
''' 

class RotationApp(App): 
    def build(self): 
     return Builder.load_string(button1) 
    def build2(self): 
     return Builder.load_string(button2) 

RotationApp().run() 

回答

1

當你的應用程序運行時,kivy運行build方法,並使用返回的部件爲根小部件。在你的情況下,你從你的button1字符串中返回按鈕,這就是它的工作完成。

問題是,kivy不知道或在意你寫了一個build2方法,它不會調用它,並且不知道如何處理返回的小部件,即使它做了。

有很多方法可以創建兩個相鄰的按鈕,我不確定您的總體目標是什麼,但一個簡單的選項就是修改您的一個kv語言字符串以在同一佈局中包含兩個按鈕。我有一個BoxLayout其自動調整他們是相鄰取代FloatLayout

button1 = ''' 
BoxLayout: 
    Button: 
     text: 'Check In' 
     size_hint: None, None 
     pos_hint: {'center_x': .5, 'center_y': .5} 
     canvas.before: 
      PushMatrix 
      Rotate: 
       angle: 0 
       origin: self.center 
     canvas.after: 
      PopMatrix 
    Button: 
      text: 'SOS' 
      size_hint: None, None 
      pos_hint: {'center_x': 1.5, 'center_y': 1.5} 
      canvas.before: 
       PushMatrix 
       Rotate: 
        angle: 45 
        origin: self.center 
      canvas.after: 
       PopMatrix 

''' 

我沒有試過這種代碼,你可以從你的旋轉得到一些怪異的行爲/重疊,但它是正確的總體思路放置相鄰的小部件並讓它們都顯示出來。

+0

你的代碼非常好!謝謝 !!我可以看到兩個按鈕,但它們顯示在左側,而我需要它們在中心或底部。你知道怎麼做嗎?另外,我搜索了kivy文檔,但無法找到正確的方式來顯示單擊此按鈕之一的文本。在此先感謝您的幫助。 – user2922822

+0

要更改它們的位置,請更改佈局參數。正確的做法取決於程序的其餘部分,但如果您切換回FloatLayout,則使用pos_hint的最初想法是一種方法。 – inclement