3

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

How can I update my code so that by pressing the help_button, main_label changes its text ?

Thank you for your help.

G4bri3l
  • 4,996
  • 4
  • 31
  • 53
frg100
  • 139
  • 1
  • 2
  • 9

3 Answers3

4

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

Improvement: 1

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

Improvement: 2

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)

Improvement: 3

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"

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()
kiok46
  • 1,704
  • 1
  • 12
  • 28
  • Thanks for your help! What does the event do in the update function? – frg100 Jun 16 '15 at 23:15
  • Also, how would I add a function so I can change the text I want main_label to output? (So it can be different for each of my buttons) – frg100 Jun 16 '15 at 23:17
  • It passes instance of that button, try this `print event.text` in update function. – kiok46 Jun 17 '15 at 07:46
  • Personally when I deal with such a situation, I love to use kivy language, using ids is always helpful. but since you asked. you can do it this way add this in update function. `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` but this is not a good method as text on the button could change. – kiok46 Jun 17 '15 at 07:49
  • if you want to know about playing with ids in kivy language, then this link might help. http://stackoverflow.com/questions/30202801/how-to-access-id-widget-of-different-class-from-a-kivy-file-kv – kiok46 Jun 17 '15 at 07:52
1

Well another way you can go about it is that in Kivy everything at the right of the kivy language properties is pure python. So you can connect your kivy files to the python by passing some tag to your function and then act in python how ever you want.

In the .kv file:

Button: 
   on_press: root.label_change('btn1')

In the Python file:

Class MyButton(Button): 

    def label_change(self, event):
     self.event = event 

        if self.event == "btn1": 
           label.text = "something"
MagTun
  • 5,619
  • 5
  • 63
  • 104
user2067030
  • 754
  • 1
  • 10
  • 27
0

Small dirty snippet!

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()
SagitSri
  • 491
  • 6
  • 15