Ed Lancaster
Ed Lancaster

Reputation: 3

Fixing error: 'StringVar' object not callable in Python

I am currently trying to refresh the content in an entry field in python, below is my code:

from tkinter.filedialog import *

from tkinter import *
master = Tk()

menu = Menu(master)
master.config(menu=menu)

Label(master, text='Camera File:').grid(row=0, column=1)

def carddir_get():
    temp_carddir = askdirectory(parent=master, title='Please select a directory')
    print(temp_carddir)
    cdg.delete(0,END)
    cdg.insert(0,temp_carddir)

temp_carddir = StringVar()
temp_carddir.set("/path/to/card/")
temp_carddir.trace("w", temp_carddir)

cdg = Entry(master, textvariable=temp_carddir)

cdg.grid(row=0,column=2)

Button(master, text='Browse', command=carddir_get).grid(row=0, column=3)

mainloop()`

Upvotes: 0

Views: 1085

Answers (1)

Tagc
Tagc

Reputation: 9076

As I said in the comments, I don't use Tkinter but it seems like trace expects a callback as its second parameter.

I replaced:

temp_carddir.trace("w", temp_carddir)

With:

temp_carddir.trace("w", lambda nm, idx, mode: print('nm={}, idx={}, mode={}'.format( nm, idx, mode)))

After opening and closing the dialog box, I now see the following output in the console instead of an error:

nm=PY_VAR0, idx=, mode=w

Hopefully this makes more sense to you than it does to me, and you can carry on from here.

Upvotes: 1

Related Questions