Skip to content
Advertisement

How do I have a label display a string with a text input variable using python in Kivy without the kv. file?

For context, I am creating a simple golf app. Unfortunately, I’ve run into an issue which really seems like it should be easy to solve but I can’t figure it out. I’m trying to have the 3rd screen (RegisterWindow) say “Hi (the username the user inputs in the screen before)” by adding the variable I have set to whatever the user puts in, which is username. However, the code I have below gives me an attribute error. How do I fix this issue without using a kv. file?

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.core.window import Window


class ScreenManagement(ScreenManager):
    def __init__(self, **kwargs):
        super(ScreenManagement, self).__init__(**kwargs)


class LoadingWindow(Screen):
    def __init__(self, **kwargs):
        super(LoadingWindow, self).__init__(**kwargs)
        self.add_widget(Label(text='Welcome To The Putting Analyzer', font_size=100, size_hint=(.0, .0),
                              pos_hint={'x': .5, 'top': .6}))

        self.submit = Button(text="Continue", font_size=50, background_color=(160 / 255, 197 / 255, 196 / 255, 0),
                             size_hint=(.15, .15), pos_hint={'center_x': .5, 'y': .4})
        self.add_widget(self.submit)
        self.submit.bind(on_press=self.pressed)
        self.submit.bind(on_press=self.screen_transition)

    def pressed(self, instance):
        pass

    def screen_transition(self, *args):
        self.manager.current = 'login'


class LoginWindow(Screen):
    def __init__(self, **kwargs):
        super(LoginWindow, self).__init__(**kwargs)
        self.add_widget(
            Label(text='First, enter a username: ', font_size=50, size_hint=(.0, .1), pos_hint={'x': .23, 'y': .7}))
        self.username = TextInput(multiline=False, size_hint=(.45, .1), pos_hint={'x': .45, 'y': .7})
        self.add_widget(self.username)

        self.add_widget(
            Label(text='Next, enter your handicap: ', font_size=50, size_hint=(.0, .1), pos_hint={'x': .25, 'y': .5}))
        self.handicap = TextInput(multiline=False, size_hint=(.45, .1), pos_hint={'x': .45, 'y': .5})
        self.add_widget(self.handicap)

        self.add_widget(Label(text="*If you don't have a handicap or don't nknow what your handicap is, enter N/A",
                              color=(1, 0, 0), font_size=35, size_hint=(.0, .1), pos_hint={'x': .253, 'y': .44}))

        self.submit2 = Button(text="Continue", font_size=50, background_color=(160 / 255, 197 / 255, 196 / 255, 0),
                              size_hint=(.15, .15), pos_hint={'center_x': .5, 'y': .3})
        self.add_widget(self.submit2)
        self.submit2.bind(on_press=self.pressed)
        self.submit2.bind(on_press=self.screen_transition)

    def pressed(self, instance):
        username = self.username.text
        handicap = self.handicap.text
        print("Username:", username, "Handicap:", handicap)

    def screen_transition(self, *args):
        self.manager.current = 'register'


class RegisterWindow(Screen):
    def __init__(self, **kwargs):
        super(RegisterWindow, self).__init__(**kwargs)

        self.add_widget(Label(text = "Hi" + username))


class Application(App):
    def build(self):
        Window.clearcolor = (160 / 255, 197 / 255, 196 / 255)
        sm = ScreenManagement(transition=SlideTransition())
        sm.add_widget(LoadingWindow(name='start'))
        sm.add_widget(LoginWindow(name='login'))
        sm.add_widget(RegisterWindow(name='register'))
        return sm


if __name__ == "__main__":
    Application().run()

Advertisement

Answer

There are several ways to achieve that. One of the shortest way could be assigning the value directly to the Label instance of RegisterWindow.

For that first keep there a reference of the Label as,

class RegisterWindow(Screen):
    def __init__(self, **kwargs):
        super(RegisterWindow, self).__init__(**kwargs)
        self.user_name_label = Label()
        self.add_widget(self.user_name_label)

Then use any one of the methods (say, screen_transition) of your LoginWindow (that you have bound to the Button) to pass the desired values as,

    def screen_transition(self, *args):
        username = self.username.text
        handicap = self.handicap.text
        print("Username:", username, "Handicap:", handicap)
        self.manager.current = 'register'
        # Access the currently displayed screen.
        register_window = self.manager.current_screen
        # Assign the text prop. of the specific label.
        register_window.user_name_label.text = "Hi "+username
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement