Reputation: 293
The first set of code works. It displays the updating variable. However, in the second code, when I put the variable in a Treeview widget instead, the variable just stays on 0 and does not update. How come this works with the label but not with treeview? What is the easiest way to fix this?
First (works):
from tkinter import *
from tkinter import ttk
import threading
import time
def fun():
for i in range(10):
var.set(var.get() + 1)
time.sleep(.5)
t = threading.Thread(target=fun)
root = Tk()
var = IntVar()
var.set(0)
mainframe = ttk.Frame(root)
mainframe.grid(column = 0, row = 0)
label = ttk.Label(mainframe, textvariable=var)
label.grid(column = 0, row = 0)
t.start()
root.mainloop()
Second (does not work):
from tkinter import *
from tkinter import ttk
import threading
import time
def fun():
for i in range(10):
var.set(var.get() + 1)
time.sleep(.5)
t = threading.Thread(target=fun)
root = Tk()
var = IntVar()
var.set(0)
mainframe = ttk.Frame(root)
mainframe.grid(column = 0, row = 0)
tree = ttk.Treeview(mainframe, columns = ('number'), height = 1)
tree.insert('', 'end', text = 'Number', values = var.get())
tree.grid(column=0, row=0)
t.start()
root.mainloop()
Upvotes: 1
Views: 1982
Reputation: 2231
modify fun
to :
def fun():
for i in range(10):
var.set(var.get() + 1)
x = tree.get_children()
tree.item(x, text = 'Number', values = var.get())
time.sleep(.5)
The get_children method returns a list of tuples item IDs, one for each child of the tree. With tree.item then update the child with the required id.
In the second program along with the variable var
, you also have to update the child of the tree
Upvotes: 1