Search code examples
pythonclasstkintertkinter-entry

Get Entry text in another class


I am working with Tkinter on python 2.7.8

I have two classes and each of them creates a window. In the Interface class the root window is created and call the second window with top level function.

The root window has an Entry and the second window has just a label, so I would like to give the entry (of the root window) to the label( in the second window) in real time, so I don't want to click on a Button to give the text.

I tried:

from Tkinter import *

class Interface(Frame):


    def __init__(self, fenetre, **kwargs):
        Frame.__init__(self, fenetre, width=0, height=0, **kwargs)
        self.pack(fill=BOTH)

        self.textA = StringVar()
        self.textE = Entry(self, textvariable=self.textA, width=30)
        self.textE.pack()
        self.getT = self.textA.get()

        self.newWindow = Toplevel(fenetre)
        self.app = Interface2(self.newWindow)

class Interface2(Frame):


    def __init__(self, fenetre, **kwargs):
        Frame.__init__(self, fenetre, width=0, height=0, **kwargs)
        self.pack(fill=BOTH)
        fenetre.geometry("700x700")

        self.textInRealTime = Label(self, text=interface.getT) #NameError: global name 'interface' is not defined

        self.textInRealTime.pack()


fenetre = Tk()
interface = Interface(fenetre)

interface.mainloop()
interface.destroy()

so, in the Interface2 class at the commented line I tried this text=interface.getT and got

NameError: global name 'interface' is not defined

I also tried this text=Interface.getT and got this error #AttributeError: class Interface has no attribute 'getT'

How can I get the entry on the first class to the second class in real time?


Solution

  • @mhawke, thank you for how to pass variable to other class !! so YES I did it !! transfer the text to an another window in real time with a simple thread :) here is the code:

    from Tkinter import *
    from threading import Thread
    
    def updateA(textA, tempText):
        while 1:
            tempText.set(textA.get())
    
    class Interface(Frame):
    
    
        def __init__(self, fenetre, **kwargs):
            Frame.__init__(self, fenetre, width=0, height=0, **kwargs)
            self.pack(fill=BOTH)
    
            self.textA = StringVar()
            self.textE = Entry(self, textvariable=self.textA, width=30)
            self.textE.pack()
    
    
            self.newWindow = Toplevel(fenetre)
            self.app = Interface2(self.newWindow, self.textA)
    
    class Interface2(Frame):
    
    
        def __init__(self, fenetre, textA, **kwargs):
            Frame.__init__(self, fenetre, width=0, height=0, **kwargs)
            self.pack(fill=BOTH)
            fenetre.geometry("700x700")
    
            self.tempText = StringVar()
            self.textW = Label(self, textvariable=self.tempText)
            self.textW.pack()
            t = Thread(target=updateA, args=(textA,self.tempText))
            t.start()
    
    
    fenetre = Tk()
    interface = Interface(fenetre)
    
    interface.mainloop()
    interface.destroy()