当$ P $在Kivy pssing一个按钮Python的更新标签的文本 [英] Update label's text when pressing a button in Kivy for Python

查看:996
本文介绍了当$ P $在Kivy pssing一个按钮Python的更新标签的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:我想打一个游戏,当你preSS一个按钮,但我到处找了一个星期,还是不明白该怎么做了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()

如何更新我的code,使得由pressing的help_button,main_label改变其文字?

感谢您的帮助。

推荐答案

嘛!有在你的code改善的实际需要。 (我把它理解为你是没有经历过。)

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

改进:1

应用程序可以,如果你的版本(返回一个widget)构建,或者如果你设置
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是pressed,更新函数被调用它改变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"

下面是你充满改进code

Here is you full improved code

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

这篇关于当$ P $在Kivy pssing一个按钮Python的更新标签的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆