2013-07-23 53 views
1

我正在與Kivy第一次開發移動應用程序。我已經取得了一些進展,我希望有一個「主」頁面出現在其他任何地方之前。用戶然後按下一個按鈕,頁面會變成更有用的東西。用鼠標點擊更改/更新應用程序 - kivy

目前,我得到一個巨大的按鈕,當我點擊它沒有任何反應......

我是新來kivy任何幫助將是真棒。

我試着用我「認爲」發生的事情評論我的代碼。

from kivy.uix.widget import Widget 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.carousel import Carousel 
from kivy.uix.label import Label 
from kivy.uix.button import Button 
from kivy.graphics import Color 
from kivy.app import App 
from kivy.graphics.instructions import CanvasBase 

CAROUSEL_OUTPUT = [0,1,2,3,4,5,6,7,8,9] 

class MyApp(App): 
    def build(self): 
     #for starters just show a big button that will call 
     #showData when clicked. 
     b = Button(text='Click Me!!!') 
     self.layout = GridLayout(cols=1,rows=4,spacing=[2,0]) 
     self.layout.add_widget(b) 
     #i pass the layout thinking that i can it 
     #not sure what i need to change to make it work 
     b.bind(on_press=(lambda e=1:self.showData(self.layout)))   
     return self.layout 

    def showData(self,layout): 
     self.Money = [] 
     self.Trip = [] 
     self.Gals = [] 
     #set up the local layout 
     layout = GridLayout(cols=1,rows=4,spacing=[2,0]) 
     row1 = BoxLayout(orientation='vertical') 
     row2 = BoxLayout(orientation='vertical') 
     row3 = BoxLayout(orientation='vertical') 
     w = self.makeCarousels(6,4,1) 
     l = Label(text='Please enter the total amount paid.') 
     row1.add_widget(l) 
     row1.add_widget(w) 
     layout.add_widget(row1) 
     w = self.makeCarousels(7,3,2) 
     l = Label(text='Please enter the total amount of gallons of gasoline purchased.') 
     row2.add_widget(l) 
     row2.add_widget(w) 
     layout.add_widget(row2) 
     w = self.makeCarousels(5,4,3) 
     b = Button(text='Click Me!!!') 
     b.bind(on_press=(lambda e=1: self.printCindexes())) 
     l = Label(text='Please enter the miles driven on your last tank.(Trip)') 
     row3.add_widget(l) 
     row3.add_widget(w) 
     layout.add_widget(row3) 
     layout.add_widget(b) 
     self.layout = layout 
     return layout 

    def makeCarousels(self,numOfCarous,placeHolder,row): 
     #this function just makes numOfCarous carousels 
     #and puts a '.' at placeHolder 
     check = False 
     layout = BoxLayout(orientation='horizontal') 
     for i in range(0,numOfCarous): 
      if i == (placeHolder - 1): 
       check = True 
       c = Carousel(direction = 'top') 
      else: 
       c = Carousel(direction = 'top') 
      if row == 1: 
       self.Money.append(c) 
      elif row == 2: 
       self.Gals.append(c) 
      elif row == 3: 
       self.Trip.append(c) 
      for num in CAROUSEL_OUTPUT: 
       l = Label(text=str(num)) 
      c.add_widget(l) 
     if check: 
      l = Label(text='.') 
      layout.add_widget(c) 
      layout.add_widget(l) 
      check = False 
     else:   
      layout.add_widget(c)     
     return layout 

    def printCindexes(self): 
     self.calculateValues(self.Money,4,1) 
     self.calculateValues(self.Gals,3,2) 
     self.calculateValues(self.Trip,4,3) 
     print '\n' 

    def calculateValues(self,list,placeHolder,row): 
     numOfEntries = len(list) 
     total = 0.0 
       factor = 1.0 
     for n in range(0,placeHolder-1): 
      factor=factor*10.0 
     for n in list: 
      total += factor*n.index 
      factor = factor/10.0 
     if row == 1: 
      print 'Total Paid: $%6.2f' %(total) 
     elif row == 2: 
      print 'Total Gallons: %7.4f gallons' %(total) 
     elif row == 3: 
      print 'Total Trip: %5.1f miles' %(total) 


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

再次感謝大家!!!!

回答

3

你的showData方法的第五行是layout = GridLayout(cols=1,rows=4,spacing=[2,0])。這覆蓋您傳遞給函數的變量'佈局' - python忘記了第一個layout變量存在,只知道剛剛創建的新GridLayout。在這之後,所有東西都會被添加到新的佈局中,但是您不會告訴基維對它做任何事情。

如果你只是刪除該行,所有新的佈局將被添加到你的根佈局就像你想要的,並且可見主屏幕被更新以顯示它們。

所以...應該解決您的一般問題,但總體結構看起來有點不理想。爲什麼不創建一個新的課程,如class HomeGrid(GridLayout):,然後給這個課程你想改變自己的方法,如showData。這樣你就不必跟蹤類變量並將它們傳遞給它們,你可以正確地將對象轉換與對象本身關聯起來。

我也強烈推薦閱讀關於kivy語言,這確實使得佈局設計非常容易。您可以非常輕鬆地爲您的小部件製作基本模板,而不是像b.bind這樣的東西搞亂了這些模板,一旦綁定了許多功能,它們就會很快變得笨拙。

2

我完全同意@inclement。你應該使用Kivy Language。我建議你使用ScreenManager。下面我發佈一個我給出的previous answer的簡化示例。通知,只要你想,你可以有很多的屏幕和互換使用屬性current之間:

on_press: root.current = 'screen2' 

properties(或here)Kivy的比簡單的屬性更多。它們會觸發用於保持界面最新的事件。您也可以使用Kivy語言將@inclement指出的事件綁定。你只需要做這樣的事情來調用在Calc類中定義的方法:

on_press: root.product(*args) 

或者如果它是不復雜的Kivy語言甚至被處決。

on_press: _result.text = str(int(_a.text) + int(_b.text)) 

這裏是你正在努力實現類似的代碼:

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.screenmanager import ScreenManager 

Builder.load_string(""" 
<[email protected]>: 
    a: _a 
    b: _b 
    result: _result 
    Screen: 
     name: 'screen1' 
     Button: 
      size_hint: .3,.1 
      center: self.parent.center 
      text: 'Go to Screen 2' 
      on_press: root.current = 'screen2' 
    Screen: 
     name: 'screen2' 
     GridLayout: 
      cols: 2 
      Label: 
       text: 'Value 1' 
      TextInput: 
       id: _a 
       text: '3' 
      Label: 
       text: 'Value 2' 
      TextInput: 
       id: _b 
       text: '5' 
      Label: 
       text: 'Result' 
      Label: 
       id: _result 
      Button: 
       text: 'sum' 
       # You can do the opertion directly 
       on_press: _result.text = str(int(_a.text) + int(_b.text)) 
      Button: 
       text: 'product' 
       # Or you can call a method from the root class (instance of calc) 
       on_press: root.product(*args) 
      Button: 
       text: 'Go to Screen 1' 
       on_press: root.current = 'screen1' 
""") 

class Calc(ScreenManager): 
    # define the multiplication of a function 
    def product(self, instance): 
     # self.result, self.a and self.b where defined explicitely in the kv 
     self.result.text = str(int(self.a.text) * int(self.b.text)) 

class TestApp(App): 
    def build(self): 
     return Calc() 

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