如何在链接到目录的Python中更新下拉菜单值?

时间:2019-10-09 11:41:56

标签: python tkinter

我有一个下拉菜单,可以向我返回特定文件夹中的所有.jpg文件。

它是这样的:

enter image description here

但是例如,如果在使用.py文件的同时删除其中一个.jpg文件,则下拉菜单中仍包含该已删除文件的.jpg目录。如何在下拉菜单中更新/刷新新值?无需退出.py文件并重新启动

代码如下:

    # Directory
    self.directory = "//SERVER/shared_data/Technical/Label Sign Off Sheets/sign off project/To Sign"
    self.choices = glob.glob(os.path.join(self.directory, "*- to sign.jpg")) # all choices ending with - to sign.jpg in the directory above
    self.tkvar.set('...To Sign Off...') # set the default option

    # Images, placing the image onto canvas
    def change_dropdown():
        imgpath = self.tkvar.get()
        img = Image.open(imgpath)
        photo = ImageTk.PhotoImage(img)
        label2.image = photo
        label2.configure(image=photo)


    #widgets
    self.msg1 = Label(main, text = "Choose here")
    self.msg1.grid(column = 0, row = 0)
    self.popupMenu = OptionMenu(main, self.tkvar, *self.choices, command = func) #Dropdown menu of all sign off Sheets that need signing
    self.popupMenu.grid(row=1, column=0)
    self.display_label = label2 = Label(main, image=None)
    self.display_label.grid(row=2, column=0, rowspan = 500)
    self.open_button = Button(main, text="Open", command=change_dropdown) # opens the directory and opens selected image
    self.open_button.grid(row=502, column=0)
    self.resetBtn = Button(main, text = "reset", command = reset)
    self.resetBtn.grid(column = 1, row = 15, sticky = W)

0 个答案:

没有答案