Reputation: 1
How can i get the value of textvariable
from town_label1
and street_label1
after i pick country in combobox? F.E for Belgium town_label1
should be 'Brussels' and for street_label1
should be "red_boulevard".
when i print it in funtcion works perfectly, but i need those dynamic values outside of a function
Gives me PyVAR1 when i use .get()
method and when i try to print function asks me for func argument. I used lambda
in .bind()
, but it allows only 4 arguments and i need more than that, since "town" and "street" will not be the only keys later.
import tkinter as tk
import tkinter.ttk as ttk
#DICTIONARIES#
materialDict = {"": {"town": 0, "street": 0},
"Belgium": {"town": "Brussels", "street": "red_boulevard"},
"Germany": {"town": "Bremen", "street": "green_boulevard"}}
#FUNCTION#
def selected(func):
a = materialDict [main.get()]["town"]
town.set(a)
c = materialDict [main.get()]["street"]
street.set(c)
#WINDOWLOOP#
root = tk.Tk()
root.geometry("250x125")
#VARIABLES#
main = tk.StringVar()
town = tk.StringVar()
street = tk.StringVar()
#COMBOBOXES#
combobox = ttk.Combobox(root, height=5, state="readonly", values=list(materialDict.keys()), textvariable=main)
combobox.place(x=10, y=10, width=130)
combobox.bind('<<ComboboxSelected>>', func=selected)
#LABELS#
town_label = tk.Label(root, text='town:')
town_label.place(x=150, y=10, width=70, height=20)
street_label = tk.Label(root, text='street:')
street_label.place(x=150, y=70, width=70, height=20)
town_label1 = tk.Label(root, textvariable=town)
town_label1.place(x=155, y=30, width=100, height=20)
street_label1 = tk.Label(root, textvariable=street)
street_label1.place(x=155, y=90, width=100, height=20)
root.mainloop()
Upvotes: 0
Views: 1098
Reputation: 385870
When you call town_label1.cget("textvariable")
it will return the internal name of the variable rather than the variable itself. Tkinter provides a method named getvar
to get the value of a variable by name.
varname = town_label1.cget("textvariable")
value = town_label1.getvar(varname)
print(f"value: {value}")
Upvotes: 1