0

How can I assign a variable class (tkinter.StringVar(), tkinter.IntVar(), etc.) to a Toplevel or Tk 's title text? My current code is like the following:

import tkinter as tk

root = tk.Tk()
root.title("Untitled")

titleVar = tk.StringVar()
titleVar.set("Something")

def change_title():
    titleVar.set("Title changed!")
    print(titleVar.get())

tk.Button(text="Update", command=change_title).pack()

root.mainloop()
Nae
  • 14,209
  • 7
  • 52
  • 79
  • 1
    Any widget that allows the use of a var will have a separate configuration option for that: `textvariable=` instead of `text=` for a Label, for example. That's simply not the case here. – jasonharper Dec 03 '17 at 15:34
  • 1
    only some widgets have `textvariable=`. You could use `trace` in `StringVar` to execute function which will change title with `root.title(...)` but I don't see sense to use it if you can use `root.title(..)` directly in `change_title` – furas Dec 03 '17 at 16:47

1 Answers1

1

Never tried, but based on the documentation, the trace method may do something what you need.

As the linked documentation has a "FIXME" only, I would try something like

def testCallback(*args):
    root.title(titleVar.get())

titleVar.trace("w",testCallback)

After that when someone modifies titleVar it should get propagated to the title. It may be refined using suggestions from What are the arguments to Tkinter variable trace method callbacks?

However I have not tried any of this, and I also see that while this is what the title of your question seems to be about (and that is why I leave it here), in the description you seem to be more interested about getting notifications when someone else changes the title.

tevemadar
  • 12,389
  • 3
  • 21
  • 49