Abdul Nuhu
Abdul Nuhu

Reputation: 43

How to change the behaviour of a label from another class in kivy

I want to create a page, showing a form and a submit-buttton that when clicked it will take you to another page that will show the data being inputs from the form page.

I created 2 class Form and Profile, but when I clicked the button of form, it will take you to profile page but nothing is change.

I've tried so many things but couldn't do it, please help.

.py file

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen



class Manager(ScreenManager):
    pass

class Profile(Screen):
    profile_name = ObjectProperty()
    profile_matric = ObjectProperty()
    profile_school = ObjectProperty()
    profile_programme = ObjectProperty()
    profile_level = ObjectProperty()
    
    def make_changes(self):
        self.ids.profile_name = 'New name'
        

class Form(Screen):
    user_name = ObjectProperty()
    matric = ObjectProperty()
    school = ObjectProperty()
    programme = ObjectProperty()
    level = ObjectProperty()
    
    
    def submit(self):
        y = Profile()
        y.make_changes()

kv = Builder.load_file('main.kv')

class SpinApp(App):
    def build(self):
        return kv
        
if __name__ == '__main__':
    SpinApp().run()

.kv file

Manager:
    Form:
    Profile:


<Form@BoxLayout>:
    name: 'form'
    orientation: 'vertical'
    
    user_name: user_name
    matric: matric
    school: school
    programme: programme
    level: level
    
    RelativeLayout:
        GridLayout: 
            cols: 1
            #size_hint: (0.8, 0.8)
            spacing: 5
            padding: [20]
            pos_hint: {'center_x':.5, 'center_y':.5}
            background_normal: ''
        
    
            GridLayout:
                cols: 2
                Label:
                    text: '\nName'
                    size_hint_x: None
                    size: self.texture_size
                    pos_hint: {'left': 1}
                    bold: True
                    size_hint_y: None
                    height: 80
                Input_text:
                    id: user_name
                    hint_text: 'enter your full name'
            
            GridLayout:
                cols: 2     
                Input_label:
                    text: '\nMatric Number'
                Input_text:
                    id: matric
                    hint_text: 'eg. se/mat-csc/19/0000'
            
            GridLayout:
                cols: 2
                Input_label:
                    text: '\nSchool'
                Input_text:
                    id: school
                    hint_text: 'your school'
                    
            GridLayout:
                cols: 2
                Input_label:
                    text: '\nLevel'
                Input_text:
                    id: level
                    hint_text: 'Enter your level'
                
            GridLayout:
                cols: 2
                Input_label:
                    text: '\nProgramme'
                Input_text:
                    id: programme
                    hint_text: 'type your programme'
                    
            Button:
                text: 'submit'
                on_release:
                    app.root.current = 'profile'
                    root.submit()
                


<Profile@BoxLayout>:
    name: 'profile'
    orientation: 'vertical'
    
    profile_name: profile_name
    profile_matric: profile_matric
    profile_level: profile_level
    profile_school: profile_school
    profile_programme: profile_programme

    RelativeLayout:
        GridLayout: 
            cols: 1
            size_hint: (0.8, 0.8)
            spacing: 5
            padding: [20]
            pos_hint: {'center_x':.5, 'center_y':.5}
            background_normal: ''
            canvas.before:
                Color:
                    rgba:  (253/255, 245/255, 230/255, 1)
                RoundedRectangle:
                    size: self.size
                    pos: self.pos
                    radius: [15]
                    
                
            
            Image:
                source: 'slde_1.png'
                size: self.texture_size
            
            profile_label:
                id: profile_name
                text: 'Name: '
            profile_label:
                id: profile_matric
                text: 'Matric no.:'
            profile_label:
                id: profile_level
                text: 'Level:'
            profile_label:
                id: profile_school
                text: 'School:'
            profile_label:
                id: profile_programme
                text: 'Programme:'
        

<profile_label@Label>:
    color: 1,1,1,1
    bold: True
    font_size: 30
    size_hint_y: None
    height: 85
    text_size: self.size
    halign: 'left'
    valign: 'middle'
    padding: [10,0]
    canvas.before:
        Color:
            rgba:  47/255, 79/255, 79/255, 1
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: [25]

<Input_label@Label>:
    size_hint_x: None
    size: self.texture_size
    pos_hint: {'left': 1,}
    size_hint_y: None
    height: 80
    bold: True
    
<Input_text@TextInput>:
    multiline: False
    size_hint_y: None
    height: 80
    padding: [10,20]
    background_normal: ''
    background_color: 240/255, 248/255, 1, 1
    valign: 'bottom'

Upvotes: 0

Views: 141

Answers (1)

John Anderson
John Anderson

Reputation: 38947

In your Profile class you can add an on_enter() method. The on_enter() method is executed every time the Profile Screen is entered. So something like this:

class Profile(Screen):
    profile_name = ObjectProperty()
    profile_matric = ObjectProperty()
    profile_school = ObjectProperty()
    profile_programme = ObjectProperty()
    profile_level = ObjectProperty()

    def on_enter(self, *args):
        form = self.manager.get_screen('form')
        self.profile_name.text = form.user_name.text
        self.profile_matric.text = form.matric.text
        self.profile_school.text = form.school.text
        self.profile_programme.text = form.programme.text
        self.profile_level.text = form.level.text

Then in your kv, the submit Button can just change the Screen:

        Button:
            text: 'submit'
            on_release:
                app.root.current = 'profile'

Upvotes: 1

Related Questions