• <i id='sIXpf'><tr id='sIXpf'><dt id='sIXpf'><q id='sIXpf'><span id='sIXpf'><b id='sIXpf'><form id='sIXpf'><ins id='sIXpf'></ins><ul id='sIXpf'></ul><sub id='sIXpf'></sub></form><legend id='sIXpf'></legend><bdo id='sIXpf'><pre id='sIXpf'><center id='sIXpf'></center></pre></bdo></b><th id='sIXpf'></th></span></q></dt></tr></i><div id='sIXpf'><tfoot id='sIXpf'></tfoot><dl id='sIXpf'><fieldset id='sIXpf'></fieldset></dl></div>
    1. <legend id='sIXpf'><style id='sIXpf'><dir id='sIXpf'><q id='sIXpf'></q></dir></style></legend>

      <small id='sIXpf'></small><noframes id='sIXpf'>

        <bdo id='sIXpf'></bdo><ul id='sIXpf'></ul>

      1. <tfoot id='sIXpf'></tfoot>
      2. 在 Kivy for Python 中按下按钮时更新标签的文本

        Update label#39;s text when pressing a button in Kivy for Python(在 Kivy for Python 中按下按钮时更新标签的文本)
        <legend id='kpndL'><style id='kpndL'><dir id='kpndL'><q id='kpndL'></q></dir></style></legend>
      3. <small id='kpndL'></small><noframes id='kpndL'>

          <bdo id='kpndL'></bdo><ul id='kpndL'></ul>
          <i id='kpndL'><tr id='kpndL'><dt id='kpndL'><q id='kpndL'><span id='kpndL'><b id='kpndL'><form id='kpndL'><ins id='kpndL'></ins><ul id='kpndL'></ul><sub id='kpndL'></sub></form><legend id='kpndL'></legend><bdo id='kpndL'><pre id='kpndL'><center id='kpndL'></center></pre></bdo></b><th id='kpndL'></th></span></q></dt></tr></i><div id='kpndL'><tfoot id='kpndL'></tfoot><dl id='kpndL'><fieldset id='kpndL'></fieldset></dl></div>

              <tbody id='kpndL'></tbody>

                  <tfoot id='kpndL'></tfoot>
                  本文介绍了在 Kivy for Python 中按下按钮时更新标签的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  Here is my code: I want to make a game where the main_label changes text when you press a button but I've looked everywhere for a week and still don't understand how to do it. I looked on Kivy's website but I don't understand. As you can see I'm new to kivy and not very experienced

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

                  感谢您的帮助.

                  推荐答案

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

                  Well! there was a real need for improvement in your code. (I understand it as you are not experienced.)

                  改进:1

                  如果您在 build() 上返回一个小部件,或者您设置self.root.(你不应该在构建函数本身中制作所有的 gui.)

                  An application can be built if you return a widget on build(), or if you set self.root.(You shouldn't make all of the gui in build function itself.)

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

                  改进:2

                  on_release/on_press 两者总是有用的.

                  on_release/on_press both are always useful.

                  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 的文本.

                  As help_button is pressed, update function is called which changes the text of 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()
                  

                  这篇关于在 Kivy for Python 中按下按钮时更新标签的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)
                  <tfoot id='3bq3f'></tfoot>

                    <small id='3bq3f'></small><noframes id='3bq3f'>

                    • <i id='3bq3f'><tr id='3bq3f'><dt id='3bq3f'><q id='3bq3f'><span id='3bq3f'><b id='3bq3f'><form id='3bq3f'><ins id='3bq3f'></ins><ul id='3bq3f'></ul><sub id='3bq3f'></sub></form><legend id='3bq3f'></legend><bdo id='3bq3f'><pre id='3bq3f'><center id='3bq3f'></center></pre></bdo></b><th id='3bq3f'></th></span></q></dt></tr></i><div id='3bq3f'><tfoot id='3bq3f'></tfoot><dl id='3bq3f'><fieldset id='3bq3f'></fieldset></dl></div>

                          • <bdo id='3bq3f'></bdo><ul id='3bq3f'></ul>
                              <tbody id='3bq3f'></tbody>
                          • <legend id='3bq3f'><style id='3bq3f'><dir id='3bq3f'><q id='3bq3f'></q></dir></style></legend>