I have two comboboxes and I want to update the values of one based on the value in another box. Here is my code:
import tkinter as tk
import tkinter.ttk as ttk
def update_box(imagesbox, new_values):
imagesbox["values"] = new_values
def update_combobox_data(imagesbox, filterbox):
new_values = []
if filterbox.get() == "Bilateral":
new_values = ["Original", "Contours", "Bilateral", "Enhanced", "Greyscale", "Black and white"]
if filterbox.get() == "Median":
new_values = ["Original", "Contours", "Median", "Enhanced", "Greyscale", "Black and white"]
elif filterbox.get() == "Gaussian":
new_values = ["Original", "Contours", "Gaussian", "Enhanced", "Greyscale", "Black and white"]
imagesbox.set("")
update_box(imagesbox, new_values)
def labelframe(root, text, row, col):
label_frame = tk.LabelFrame(root, text=text)
label_frame.grid(row=row, column=col, padx=15, pady=10)
return label_frame
def label(root, text, row, col):
lab = tk.Label(root, text=text)
lab.grid(row=row, column=col, padx=15, pady=10)
return lab
def combobox(root, values, row, col):
box1 = ttk.Combobox(root, values=values, state="readonly")
box1.grid(row=row, column=col, padx=15, pady=10)
box1.set(values[0])
return box1
def main():
root = tk.Tk()
root.geometry("500x300")
frame = tk.Frame(root)
frame.pack()
labelframe1 = labelframe(frame, "Settings", 1, 0)
label1 = label(labelframe1, "Choose filter", 0, 0)
filters = ["Bilateral", "Median", "Gaussian"]
filter_combobox = combobox(labelframe1, filters, 1, 0)
label2 = label(labelframe1, "Image type", 0, 1)
images = ["Original", "Contours", "Bilateral", "Enhanced", "Greyscale", "Black and white"]
images_combobox = combobox(labelframe1, images, 1, 1)
bind1 = lambda: update_combobox_data(images_combobox, filter_combobox)
images_combobox.bind("<<ComboboxSelected>>", bind1)
root.mainloop()
main()
When I choose filter, I expect the image type box to update so that the filter gets updated there. For example, if I choose Gaussian filter, image types should be Original, Contours, Gaussian, Enhanced, Greyscale and Black and white. However, the box is not updating. I found some kind of solution, and in my code I tried to make a similar implementation, but it does not work.
Also, if I change the image type, I get this strange error: TypeError: main.<locals>.<lambda>() takes 0 positional arguments but 1 was given. I tried to search from the web but I do not understand why this error appears. I found this answer, but my lambda function does not take any arguments so there shouldn't be a problem?
How could I fix these errors?
The following changes to your code works for me.
The problem dealt with binding event and wrong combobox call.
Binding some action to a function passes
eventSo changeupdate_comboboxto includeeventThen change bind1 to include event
Also bind
filter_comboboxto bind1