0

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

1
  • you can access user input from any function using Base_Path.get() so what exactly is your problem? Commented Mar 16, 2021 at 11:01

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.