Home > front end >  how do i get information from 1 class to another
how do i get information from 1 class to another

Time:09-26

New to python - kivy - gui

im trying to get information from 1 class to another, the classes is basically the different screens i have for my GUI. ive research the return function but its not helping at all because im a noob.

main GUI running on .kv file this is a breakdown of my code.

PROJECT_PATH = ""

class TrainNew1(Screen):
    #takes user input,i click a button to submit, runs this function.
    def test(self):
        PROJECT_PATH = self.ids.ProjectName.text 
        #will print PROJECT_PATH fine within test /class function
class TrainNew2(Screen):

    print(PROJECT_PATH) # will not print

Im not sure how to get it to print in the new class.

CodePudding user response:

What you Need is global variables. Did you know about scopes? In Short Global variable is a type of variable that can be accessed/Modified from anywhere in the code file. Here's an Example :

PROJECT_PATH = ""

class TrainNew1(Screen):
    global PROJECT_PATH # this is required to modify the original PROJECT_PATH
    #takes user input,i click a button to submit, runs this function.
    def test(self):
        PROJECT_PATH = self.ids.ProjectName.text 
        #will print PROJECT_PATH fine within test /class function
class TrainNew2(Screen):
    global PROJECT_PATH # this is used to access PROJECT_PATH 
    print(PROJECT_PATH) # Now, It can be used/modified even inside in this class 
  • Related