我正在尝试制作一个应用程序,如果相应的文本文件(决定值变量(发生了更改,它将更改组合框的内容。我已经准备好了这段代码,但它只在程序重新启动后更新。
#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)
感谢你的帮助和时间
注释中建议的替代方案是直接从combobox
获取值,而不是为其分配变量。
然后您可以将mod_dir_browser
功能转换为
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)
你仍然需要初始化你的combobox值,但不清楚它的意图是什么
cmb1= ttk.Combobox(window,value=oldmoddirfile,width=50)
cmb1.set('desiredvalue')