简体   繁体   English

如何在 Tkinter 中重置下拉菜单的值和不显示图像?

[英]How to reset value of dropdown menu and un-display image in Tkinter?

Currently I am working on a reset button, that sets all the content back how it looked like.目前我正在研究一个重置按钮,它将所有内容设置回它的样子。

This is how it looks like so far as an example:到目前为止,这就是它的样子:

def reset():
   box1.deselect()
   box2.deselect() #unticks all checkboxes
   self.sendingBTN['state'] = 'disabled' #disables buttons back how I needed them to be

Now I am struggling how to set the dropdown value back to default.现在我正在努力如何将下拉值设置回默认值。 My dropdown menu values consist of paths of all jpg files inside a specific folder.我的下拉菜单值包含特定文件夹内所有 jpg 文件的路径。

I have already assigned it a default value of:我已经为它分配了一个默认值:

self.vars.set('All JPGs listed here..') # set the default option

But, when I choose a value from the drop down menu, default value dissaperars and that's why I am figuring out how to reset it.但是,当我从下拉菜单中选择一个值时,默认值会消失,这就是我想知道如何重置它的原因。

Also, I am not too sure how to remove the Image from displaying.另外,我不太确定如何从显示中删除图像。

Any help will be appreciated!任何帮助将不胜感激!

Here is my code:这是我的代码:

self.vars = StringVar()

# Directory
self.directory = "C:/Users/spice/Desktop/allFiles/"
self.choices = glob.glob(os.path.join(self.directory, "*.jpg"))
self.vars.set('All JPGs listed here..') # set the default option

# Images
def change_dropdown():
    imgpath = self.vars.get()
    img = Image.open(imgpath)
    photo = ImageTk.PhotoImage(img)
    label2.image = photo
    label2.configure(image=photo)
    self.CaptureScreen['state'] = 'normal'




#return path value
self.p = None
def function2(value):
    global p
    self.p = Path(value)
    print(self.p)
#reset values
def reset(): 
    self.Checkbox1.deselect()
    self.Checkbox2.deselect()
    self.Checkbox3.deselect()
    self.Checkbox4.deselect()
    self.Checkbox5.deselect()
    self.Checkbox6.deselect()
    self.Checkbox7.deselect()
    self.Checkbox8.deselect()
    self.Checkbox9.deselect()
    self.Checkbox10.deselect()
    self.Checkbox11.deselect()
    self.Checkbox12.deselect()
    self.CaptureScreen['state'] = 'disabled'
    self.dataSend['state'] = 'disabled'


#widgets
self.msg1 = Label(main, text = "All files here")
self.msg1.grid(column = 0, row = 0)
self.popupMenu = OptionMenu(main, self.vars, *choices, command = function2)
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)
self.open_button.grid(row=3, column=0)
self.resetBtn = Button(main, text = "reset", command = reset)
self.resetBtn.grid(column = 1, row = 2, sticky = W)

this is what I have tried to put into def reset()这就是我试图放入def reset()

        change_dropdown.delete(label2)

I have managed to remove the image by using this:我已经设法通过使用这个来删除图像:

config(image='')

Next, I will try to clear the dropdown content..接下来,我将尝试清除下拉内容..

For resetting your drop-down menu to default value, you can try the following code:要将下拉菜单重置为默认值,您可以尝试以下代码:

def reset(): 
    self.vars.set("All JPGs listed here..")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM