Search code examples
pythontkintertoplevel

Python Tkinter or ttk, Send text to other toplevel window


I am using a separate top level window during development of my Python ttk code, and would like to send messages from root to debug top level window.

Here is some pseudo code

from Tkinter import *
import ttk
from MyFunctions import *    # some code I wrote (see below)
root = Tk()
SetupMyFunct(root)           # see below
de_bug = Toplevel(root)
dbug_frame = Frame(de_bug).grid()
debug_string1 = StringVar()
debug1_window = ttk.Label(dbug_frame, textvariable = debug_string1).grid()
root.mainloop()

In my MyFunctions.py:

from Tkinter import *
import ttk
def SetupMyFunct(root):
    f = ttk.Frame(root).grid()
    w = Frame(f).grid()

At this point I would like to send some text to be automatically updated in the de_bug window, but I really have no idea where to go from here.

Help Please? Mark.


Solution

  • Chaining the geometry management method prevents you from retaining a reference to those widgets, as those methods return None to be stored in those variables. Stop doing that. This will allow you to do things like work with references to your widgets. Specifically, you will be able to specify dbug_frame as the parent object for the debug1_window widget.

    from Tkinter import *
    import ttk
    from MyFunctions import *
    root = Tk()
    f,w = SetupMyFunct(root)
    de_bug = Toplevel(root)
    dbug_frame = Frame(de_bug)
    dbug_frame.grid()
    debug_string1 = StringVar()
    debug1_window = ttk.Label(dbug_frame, textvariable=debug_string1)
    debug1_window.grid()
    root.mainloop()
    

     

    from Tkinter import *
    import ttk
    def SetupMyFunct(root):
        f = ttk.Frame(root)
        f.grid()
        w = Frame(f)
        w.grid()
        return f,w