【问题标题】:Update label's text when pressing a button in Kivy for Python在 Kivy for Python 中按下按钮时更新标签的文本
【发布时间】:2015-09-01 16:08:15
【问题描述】:

这是我的代码:我想制作一个游戏,当您按下按钮时,main_label 会更改文本,但我已经到处看了一个星期,但仍然不明白该怎么做。我查看了 Kivy 的网站,但我不明白。如您所见,我是 kivy 新手,经验不足

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 更改其文本?

感谢您的帮助。

【问题讨论】:

  • 你能解释一下你想要什么吗?

标签: python python-2.7 button label kivy


【解决方案1】:

好吧!您的代码确实需要改进。 (我理解你没有经验。)

改进: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()

【讨论】:

  • 感谢您的帮助!事件在更新函数中做了什么?
  • 另外,我将如何添加一个函数以便我可以更改我希望 main_label 输出的文本? (所以我的每个按钮都可以不同)
  • 它传递了那个按钮的实例,在更新函数中试试这个print event.text
  • 就个人而言,当我处理这种情况时,我喜欢使用 kivy 语言,使用 ids 总是有帮助的。但既然你问了。您可以通过这种方式将其添加到更新功能中。 if(event.text == "Help"): self.main_label.text = "Help button to change button" elif(event.text == "Go" ): self.main_label.text = "Go button to change button" else: pass 但这不是一个好方法,因为按钮上的文本可能会改变。
  • 如果你想知道如何用 kivy 语言玩 ids,那么这个链接可能会有所帮助。 stackoverflow.com/questions/30202801/…
【解决方案2】:

另外一种方法是,在 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"

【讨论】:

    【解决方案3】:

    小脏sn-p!

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

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 2017-07-20
      相关资源
      最近更新 更多