Ideally, I would like to use a built-in approach like tk.StringVar
to modify an array (of strings) in a button command, but also need to the modified array to live on so it can be modified in other function calls. The line print(a)
in the code below prints an array-like object ('abc', 'gh', 'rstu')
, but the line print(a(1))
fails to return gh
and the line a.append('xyz')
returns an error, suggesting that I can’t directly hijack tk.StringVar
to carry the array. What is then my best option, if I should avoid the perils of making the array global and the overhead of defining a class?
import tkinter as tk
def append():
a = a_var.get()
print(a)
print(a(1))
a.append('xyz')
a_var.set(a)
window = tk.Tk()
a = ('abc','gh','rstu')
a_var = tk.StringVar(value=a)
tk.Button(window, text='Append', command=append).pack()
window.mainloop()