99Valk
99Valk

Reputation: 243

Tkinter window title as a variable

I want to be able to dynamically change the window title based on the data being displayed in the window. I display a window with a selected weather alert. In the window title, I want to put the alert event name. Each selected alert will have its own event name.

I created a small test to show using a variable in the title works.

from tkinter import *

root = Tk()
mytitle='MYTITLE'
root.title(mytitle)
root.mainloop()

However when I apply what I think is the same setup in my popup window I get title "PY_VAR0".

def msgwindow(x):
    print('in message window')
    alert_info = Toplevel(root)  # Child window
    alert_info.geometry("600x700")  # Size of the window
    alert_row = alert_tree.item(alert_tree.focus())  # selected value to display

    alert_row_title = tk.StringVar()
    alert_row_title.set(alert_row['values'][1])
    print('value1:', alert_row['values'][1], 'title:', alert_row_title)
    alert_info.title(alert_row_title)

    alert_row_value = tk.StringVar()
    alert_row_value.set(alert_row['values'][4])

    alert_label = tk.Label(alert_info, textvariable=alert_row_value, justify=LEFT)
    alert_label.grid(row=1, column=0, sticky=N+W)

I added a 'print' which shows my title variable has the intended value but the same variable used in title shows PY_VAR0

value1: High Surf Advisory title: PY_VAR0

The the data in alert_row_value displays as intended. This must be simple...what am I doing wrong?

Upvotes: 0

Views: 739

Answers (1)

Random Davis
Random Davis

Reputation: 6857

alert_info.title() requires a str, not a StringVar. So, you need to use alert_row_title.get() to get a str that you can pass in. So, your code just needs to be:

alert_info.title(alert_row_title.get())

Upvotes: 1

Related Questions