How to use an Entry contents in a different class? How to use an Entry contents in a different class? tkinter tkinter

How to use an Entry contents in a different class?


Since your entry widget is an attribute of introwindow, you simply need to call the get method on the entry widget before you destroy the window. You can then pass the value to the constructor of the next window

For example, it would look something like this:

def clicku():    name = w2.entry.get()    print("clickU:", name)    w2.destroy()    w3 = IntroWindow(root, width=500, height=500, name=name)

Of course, you'll need to modify your IntroWindow class to accept the name.


You could do it by creating a global instance of a tkinter StringVar. Here's doing that to the code posted in your question (which I have also updated to more closely follow the PEP 8 - Style Guide for Python Code).

Your current design doesn't allow making the best use of them (which is why I made it a global variable). But for future reference, here's some additional information on how they can be used.

from tkinter import *class Window(Frame):    def __init__(self, master=None, *args, **kwargs):        Frame.__init__(self, master, *args, **kwargs)        self.pack()        self.pack_propagate(0)        self.labelone = Label(self, text="Welcome to our class! Click start to begin")        self.labelone.grid(row=1, column=1)        self.buttonone = Button(self, text="start", command=click)        self.buttonone.grid(row=2,column=1)class StartWindow(Frame):    def __init__(self, master=None, *args, **kwargs):        Frame.__init__(self, master, *args, **kwargs)        self.grid(row=5,column=5)        self.grid_propagate(0)        self.text = Label(self, text="What is your name?")        self.text.grid(row=0,column=0)        self.entry = Entry(self, width=15, textvariable=username)        self.entry.grid(row=1,column=0)        self.buttontwo = Button(self, text="enter", command=clicku)        self.buttontwo.grid(row=1,column=1)        self.username = self.entry.get()class IntroWindow(Frame):    def __init__(self, master=None, *args, **kwargs):        Frame.__init__(self,master, *args, **kwargs)        self.grid()        self.grid_propagate(0)        self.a = Label(self, text="Aki: ")        self.a.grid(row=0, column=0)        self.sp1 = Label(self, text="Hi " + username.get())        self.sp1.grid(row=0, column=1)        self.sp2 = Label(self, text="Nice to meet you!")        self.sp1.grid(row=1, column=1)        self.talk = Entry(self, width=15)def click():    global w2    w.destroy()    w2 = StartWindow(root, width=500, height=500)def clicku():    w2.destroy()    w3 = IntroWindow(root, width=500, height=500)if __name__ == '__main__':    root = Tk()    username = StringVar()    w = Window(root, bg="red", width=500, height=500)    root.mainloop()