2015-06-16 125 views
2

這裏是我的代碼:我想製作一個遊戲,當你按下按鈕時,main_label會改變文本,但我已經到處尋找了一個星期,但仍然不明白怎麼做。我看了Kivy的網站,但我不明白。正如你看到的我是新來kivy,而不是非常有經驗在Kivy for Python中按下按鈕時更新標籤的文本

from kivy.app import App 
from kivy.uix.button import Button 
from kivy.uix.label import Label 
from kivy.uix.floatlayout import FloatLayout 
from kivy.clock import Clock 

energy = 100 
hours = 4 

class app1(App): 
    def build(self): 
     self.f = FloatLayout() 

     #Labels 
     self.energy_label = Label(text = "Energy = " + str(energy), size_hint=(.1, .15),pos_hint={'x':.05, 'y':.9}) 
     self.time_label = Label(text = "Hours = " + str(hours), size_hint=(.1, .15),pos_hint={'x':.9, 'y':.9}) 
     self.name_label = Label(text = "Game", size_hint=(.1, .15),pos_hint={'x':.45, 'y':.9}) 
     self.main_label = Label(text = "Default_text", size_hint=(1, .55),pos_hint={'x':0, 'y':.35}) 

     #Main Buttons 
     self.inventory_button = Button(text = "Inventory", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.2}) 
     self.help_button = Button(text = "Help", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.1}) 
     self.craft_button = Button(text = "Craft", size_hint=(.3, .1),pos_hint={'x':.05, 'y':.1}) 
     self.food_button = Button(text = "Food", size_hint=(.3, .1),pos_hint={'x':.35, 'y':.2}) 
     self.go_button = Button(text = "Go", size_hint=(.3, .1),pos_hint={'x':.35, 'y':.1}) 
     self.walk_button = Button(text = "Walk", size_hint=(.3, .1),pos_hint={'x':.05, 'y':.2}) 

     def update(self, *args): 
      self.main_widget.text = str(self.current_text) 

     self.f.add_widget(self.energy_label) 
     self.f.add_widget(self.main_label) 
     self.f.add_widget(self.time_label) 
     self.f.add_widget(self.inventory_button) 
     self.f.add_widget(self.help_button) 
     self.f.add_widget(self.craft_button) 
     self.f.add_widget(self.food_button) 
     self.f.add_widget(self.go_button) 
     self.f.add_widget(self.walk_button) 
     self.f.add_widget(self.name_label) 
     self.current_text = "Default" 
     Clock.schedule_interval(update, 1) 
     return self.f 


     def update_label(input): 
      input = self.current_text 

     help_button.bind(on_press = update_label("success!")) 


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

我如何更新我的代碼,以便按help_button,main_label改變它的文字?

謝謝你的幫助。

+0

你能解釋一點點你想要什麼? – kiok46

回答

2

好吧!你的代碼真的需要改進。 (我把它理解爲你是沒有經歷過。)

改進:1

如果您在建立()返回一個小部件的應用程序可以建立,或者如果您設置 self.root(你。不應該讓所有在建函數本身的GUI)

def build(self): 
    return Hello() #That's what is done here 

改進:2

on_release/on_press都是始終USEF UL認證。

self.help_button = Button(text = "Help", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.1},on_press = self.update) 

改進:3

作爲help_button被按下時,更新函數被調用,其改變main_label的文本。

def update(self,event): 
    self.main_label.text = "Changed to change" 

這裏是你完全改進的代碼

from kivy.app import App 
from kivy.uix.button import Button 
from kivy.uix.label import Label 
from kivy.uix.floatlayout import FloatLayout 
from kivy.clock import Clock 

energy = 100 
hours = 4 

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

     self.energy_label = Label(text = "Energy = " + str(energy), size_hint=(.1, .15),pos_hint={'x':.05, 'y':.9}) 
     self.time_label = Label(text = "Hours = " + str(hours), size_hint=(.1, .15),pos_hint={'x':.9, 'y':.9}) 
     self.name_label = Label(text = "Game", size_hint=(.1, .15),pos_hint={'x':.45, 'y':.9}) 
     self.main_label = Label(text = "Default_text", size_hint=(1, .55),pos_hint={'x':0, 'y':.35}) 

    #Main Buttons 
     self.inventory_button = Button(text = "Inventory", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.2}) 
     self.help_button = Button(text = "Help", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.1},on_press = self.update) 
     self.craft_button = Button(text = "Craft", size_hint=(.3, .1),pos_hint={'x':.05, 'y':.1}) 
     self.food_button = Button(text = "Food", size_hint=(.3, .1),pos_hint={'x':.35, 'y':.2}) 
     self.go_button = Button(text = "Go", size_hint=(.3, .1),pos_hint={'x':.35, 'y':.1}) 
     self.walk_button = Button(text = "Walk", size_hint=(.3, .1),pos_hint={'x':.05, 'y':.2}) 

     self.add_widget(self.energy_label) 
     self.add_widget(self.main_label) 
     self.add_widget(self.time_label) 
     self.add_widget(self.inventory_button) 
     self.add_widget(self.help_button) 
     self.add_widget(self.craft_button) 
     self.add_widget(self.food_button) 
     self.add_widget(self.go_button) 
     self.add_widget(self.walk_button) 
     self.add_widget(self.name_label) 
     self.current_text = "Default" 

    def update(self,event): 
     self.main_label.text = "Changed to change" 

class app1(App): 
    def build(self): 
     return Hello() 
if __name__=="__main__": 
    app1().run() 
+0

感謝您的幫助!事件在更新函數中做了什麼? – frg100

+0

另外,我將如何添加一個函數,以便我可以更改我想要main_label輸出的文本? (所以它可以爲我的每個按鍵的不同) – frg100

+0

它通過該按鈕的情況下,嘗試在更新功能這個'打印event.text'。 – kiok46

1

那麼另一種方式,你可以去一下的是,在Kivy一切在kivy語言特性的右邊是純Python。所以你可以通過傳遞一些標籤到你的函數來將你的kivy文件連接到python,然後用python進行操作。

在.kv文件:

Button: 
    on_press: root.label_change('btn1') 

在Python的文件:

Class MyButton(Button): 

    def label_change(self, event): 
    self.event = event 

     if self.event == "btn1": 
      label.text = "something"