Search code examples
pythontkintertkinter-entry

Storing entry input from GUI


Lets say I have this code

import math
from tkinter import *


def close_window():
    root.destroy()

def fileName():
    filename = content.get()
    return filename;



root = Tk()
content = StringVar()

L2 = Label(root, text = "The Program").grid(row = 0, sticky = E)


L1 = Label(root, text = "Enter filename").grid(row = 1, column = 0, sticky =    E)
E1 = Entry(root, bd = 5, textvariable = content).grid(row = 1, column = 1)


B1 = Button(root, text = "Ok", command = fileName).grid(row = 2, column = 0 )
B2 = Button(root, text = "Quit", command = close_window).grid(row = 2, column    = 1)


root.mainloop()

print(fileName())

Now the problem is I want to store the content I enter in E1 (so I later can do things to it) but how do I access it "outside" of the GUI?

The program I want to make is that the user enters a file name, then it runs a bunch of functions on the input and then produce a textmessage based on whats given, but I cant access the input since fileName() doesnt return anything.


Solution

  • Not sure if this does what you wanted but now it prints on button click and you have you filename variable set to content.get()

    import math
    from tkinter import *
    
    
    def close_window():
        root.destroy()
    
    def fileName():
        filename = content.get()
        return filename;
    
    def combine_funcs(*funcs):
        def combined_func(*args, **kwargs):
            for f in funcs:
                f(*args, **kwargs)
        return combined_func
    
    def prnt():
        print(content.get())
    
    root = Tk()
    content = StringVar()
    
    L2 = Label(root, text = "The Program").grid(row = 0, sticky = E)
    
    
    L1 = Label(root, text = "Enter filename").grid(row = 1, column = 0, sticky =    E)
    E1 = Entry(root, bd = 5, textvariable = content).grid(row = 1, column = 1)
    
    
    B1 = Button(root, text = "Ok", command = combine_funcs(fileName,prnt)).grid(row = 2, column = 0 )
    B2 = Button(root, text = "Quit", command = close_window).grid(row = 2, column    = 1)
    
    
    root.mainloop()
    
    
    print(fileName())