1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
lokoum
  • 71
  • 10

2 Answers2

1

@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()
lokoum
  • 71
  • 10
0

Pass the textA StringVariable from Interface to Interface2.__init__() and in turn pass that on to Label() using textvariable:

Any change to the associated StringVariable will result in the label being immediately updated. There is some info in this answer https://stackoverflow.com/a/1918054/21945. Here is some working code:

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, self.textA)    # note extra StringVariable argument passed to Interface2 constructor

class Interface2(Frame):
    def __init__(self, fenetre, textA, **kwargs):    # note extra StringVariable parameter "textA"
        Frame.__init__(self, fenetre, width=0, height=0, **kwargs)
        self.pack(fill=BOTH)
        fenetre.geometry("700x700")

        self.textInRealTime = Label(self, textvariable=textA)    # note textvariable set to StringVariable
        self.textInRealTime.pack()

fenetre = Tk()
interface = Interface(fenetre)

interface.mainloop()
interface.destroy()
Community
  • 1
  • 1
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • Thank you, now the text Entry is now passed to the second class.But for the real Time update of the label can I use a Thread ? This thread will just call self.textA.get() and pass it to the second class ? I am going to try it – lokoum Jul 05 '14 at 18:43