Search code examples
pythontkintertkinter-entry

Accessing variable from outside the function in GUI (Tkinter) Python


I have a simple GUI in python. I want to print the value entered by the user from outside the function. The code I made is given below


def save_BasePath():

    Base_Path_info = Base_Path.get()
    #print(Base_Path_info)

    file_basepath = open("basepath.txt", "w")

    file_basepath.write(str(Base_Path_info))


    file_basepath.close()

app = Tk()
app.geometry("500x500")

app.title("Python File Handling in Forms")

heading = Label(text="Input Print", fg="black", bg="yellow", width="500",
                height="3", font="10")

heading.pack()

Base_Path_text = Label(text="Base_Path :")
Base_Path_text.place(x=155, y=70)
Base_Path = StringVar()
Base_Path_entry = Entry(textvariable=Base_Path, width="30")
Base_Path_entry.place(x=155, y=100)
button_basepath = Button(app, text="Enter Base Path", command=save_BasePath, width="15", height="2", bg="grey")
button_basepath.place(x=175, y=125)

#I need the user input from the function here so that I can use it further

mainloop()

On Pressing the button, I get the user input. I am able to print from within the save_basepath function. But I want to access the user input from outside so that I can work on it. Any help is appreciated


Solution

  • we can use the global keyword:

    from tkinter import *
    def save_BasePath():
        global Base_Path_info
        Base_Path_info = Base_Path.get()
        #print(Base_Path_info)
    
        file_basepath = open("basepath.txt", "w")
    
        file_basepath.write(str(Base_Path_info))
    
    
        file_basepath.close()
        print_it()
    
    def print_it():
        print(Base_Path_info)
    
    app = Tk()
    app.geometry("500x500")
    
    app.title("Python File Handling in Forms")
    
    heading = Label(text="Input Print", fg="black", bg="yellow", width="500",
                    height="3", font="10")
    
    heading.pack()
    
    Base_Path_text = Label(text="Base_Path :")
    Base_Path_text.place(x=155, y=70)
    Base_Path = StringVar()
    Base_Path_entry = Entry(textvariable=Base_Path, width="30")
    Base_Path_entry.place(x=155, y=100)
    button_basepath = Button(app, text="Enter Base Path", command=save_BasePath, width="15", height="2", bg="grey")
    button_basepath.place(x=175, y=125)
    
    #I need the user input from the function here so that I can use it further
    
    
    mainloop()
    

    But keep in mind that we want to avoid this, and the best way to do that is by using a class.

    Here is how I would do it:

    from tkinter import *
    
    class myProgram:
        def __init__(self):
            heading = Label(text="Input Print", fg="black", bg="yellow", width="500",
                            height="3", font="10")
    
            heading.pack()
    
            Base_Path_text = Label(text="Base_Path :")
            Base_Path_text.place(x=155, y=70)
            self.Base_Path = StringVar()
            Base_Path_entry = Entry(textvariable=self.Base_Path, width="30")
            Base_Path_entry.place(x=155, y=100)
            button_basepath = Button(app, text="Enter Base Path", command=self.save_BasePath, width="15", height="2", bg="grey")
            button_basepath.place(x=175, y=125)
    
        def save_BasePath(self):
            self.Base_Path_info = self.Base_Path.get()
    
            file_basepath = open("basepath.txt", "w")
    
            file_basepath.write(str(self.Base_Path_info))
    
    
            file_basepath.close()
            self.print_it()
    
        def print_it(self):
            print(self.Base_Path_info)
            
            # Continue your code here...
    
    
    app = Tk()
    app.geometry("500x500")
    
    app.title("Python File Handling in Forms")
    myProgram()
    mainloop()