0

I am trying to update a label in python. It is a simple Guess the num game. Currently it prints the Computer responses in PC but I want it to print those to a label.

here is the main .py file

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import random


secret=random.randint(1,100)

class Application(BoxLayout):
    label1=""

    def button_press(self):
        global label1
        if (int(self.user_guess.text)<secret):
            print("Higher")
            self.label1="Higher"
        elif(int(self.user_guess.text)>secret):
            print("Lower")
            self.label1="Lower"
        elif(int(self.user_guess.text)==secret):
            print("You WOn")
            self.label1="You WOn"

class WeatherApp(App):
    pass

if __name__ == '__main__':
    WeatherApp().run()

the .kv file

Application:

<Application>:
size:(480,30)
user_guess:guess
size_hint:(None,None)
text:""
orientation: "vertical"
BoxLayout:
    Button:
        text:'Play'
        on_press:root.button_press()
    TextInput:
        id:guess
        text:''
    Label:
        text:root.label1
nabeel
  • 425
  • 1
  • 9
  • 22

1 Answers1

2

I think you should use

self.label1.text="Higher"
Nhor
  • 3,860
  • 6
  • 28
  • 41