0

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()`
1
  • 1
    I don't use tkinter but the bug is in this line temp_carddir.trace("w", temp_carddir) My guess is that the second parameter should be a callback or something. Commented Jan 20, 2017 at 12:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant, thank you! That line isnt actually necessary at all, that is what was causing the issues...
@EdLancaster No worries. If you feel that my answer helped you, you could accept my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.