Home > Back-end >  update a combobox tkinter
update a combobox tkinter

Time:07-25

I am trying to make an application that changes the content of a combo box if the corresponding text file (which determines the value variable) is changed I already have this code in place but it is only updating after a program restart.

#image browser

def mod_dir_browser():
    old_moddir = oldmoddirfile
    filepath = filedialog.askdirectory(initialdir=old_moddir, title="select a new mod directory" )
    with open("mod.txt", "w", encoding="utf-8") as file:
        file.write(filepath)


#elements of the first window


button1 = Button(window, text="Change mod directory", command=lambda: [mod_dir_browser()])
button1.place(x=0, y=575)


label1 = Label(window, text='Select the game you want to install a mod to', fg = "#ffffff", bg ="#31363B" )
label1.place(x=200, y=50)

cmb1= ttk.Combobox(window,value=oldmoddirfile,width=50)
cmb1.place(x=160,y=150)

thanks for your help and time

CodePudding user response:

An alternative to the suggestion in the comments would be to get the value directly from the combobox instead of assigning it a variable.

Then you can make the mod_dir_browser function into

def mod_dir_browser():
    filepath = filedialog.askdirectory(initialdir=cmb1.get(), title="select a new mod directory" )
    cmb1.set(filepath)
    with open("mod.txt", "w", encoding="utf-8") as file:
        file.write(filepath)

You still would need to initialize your combobox value, but it is unclear what that is intended to be. However, that would be done as

cmb1= ttk.Combobox(window,value=oldmoddirfile,width=50)
cmb1.set('desiredvalue')
  • Related