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

3

这是我的代码:我想制作一个游戏,在按下按钮时更改主标签的文本,但我已经寻找了一个星期,仍然不知道如何做到。我在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更改其文本内容?
谢谢您的帮助。

你能详细解释一下你想要什么吗? - kiok46
3个回答

4

你的代码确实需要改进。(我理解你缺乏经验)

改进1:

在build()函数中,可以返回一个widget或者设置self.root来构建应用程序。(不应该在build函数中创建所有的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时,将调用update函数,该函数会更改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()

谢谢你的帮助!在更新函数中,事件是做什么的? - frg100
另外,我该如何添加一个函数,以便我可以更改我想要 main_label 输出的文本?(这样每个按钮的文本都可以不同) - frg100
它传递了该按钮的实例,请在update函数中尝试使用print event.text - kiok46
个人处理这种情况时,我喜欢使用kivy语言,使用ids总是有帮助的。但既然你问了,你可以这样做,在update函数中添加以下内容: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但这不是一个好方法,因为按钮上的文本可能会改变。 - kiok46
如果您想了解在Kivy语言中如何使用ID,那么这个链接可能会有所帮助。https://dev59.com/6V0a5IYBdhLWcg3wipPk - 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"

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接