在Kivy for Python中按下按钮时更新标签的文本

时间:2015-06-16 17:32:57

标签: python python-2.7 button label kivy

这是我的代码:我想制作一个游戏,当你按下按钮时main_label会改变文字,但我已经到处看了一周,但仍然不明白该怎么做。我查看了Kivy的网站,但我不明白。正如你所看到的,我是新来的kivy,而且不是很有经验的

PointOfInterest

如何更新我的代码,以便通过按help_button,main_label更改其文本?

感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

嘛!您的代码确实需要改进。 (我理解你,因为你没有经验。)

改进:1

如果在build()上返回一个小部件,或者如果设置,则可以构建应用程序 self.root。(你不应该在构建函数本身中创建所有gui。)

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

改进:2

on_release / on_press两者总是很有用。

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()

答案 1 :(得分: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"

答案 2 :(得分:0)

小片段!

import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
import random,string
Builder.load_string('''
<updlbl>:
    orientation: 'vertical'
    Label:
        id: updlbl
        text: 'Update Label'
    Button:
        text: 'Click me'
        on_press: root.upd('updated text')
 ''')

 class updlbl(BoxLayout):
     def __init__(self, **kwargs):
       super(updlbl,self).__init__(**kwargs)
       pass

     def upd(self,txt):
       self.ids.updlbl.text = txt




 class UpdateLabel(App):
     def build(self):
     self.title = "Update Label"
     return updlbl()

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