我想将 ttk.Combobox 转换为 CTkComboBox,但它不起作用

问题描述 投票:0回答:1

首先,我将展示原始代码,即

def apply_filter(filter):
    image = Image.open(file_path)
    width, height = int(image.width / 2), int(image.height / 2)
    image = image.resize((width, height), PIL.Image.LANCZOS)
    if filter == "Black and White":
        image = ImageOps.grayscale(image)
    elif filter == "Blur":
        image = image.filter(ImageFilter.BLUR)
    elif filter == "Sharpen":
        image = image.filter(ImageFilter.SHARPEN)
    elif filter == "Smooth":
        image = image.filter(ImageFilter.SMOOTH)
    elif filter == "Emboss":
        image = image.filter(ImageFilter.EMBOSS)
    image = ImageTk.PhotoImage(image)
    canvas.image = image
    canvas.create_image(0, 0, image=image, anchor="nw")


filter_combobox = ttk.Combobox(left_frame, 
                                values=["Black and White", "Blur",
                                        "Emboss", "Sharpen", "Smooth"])
filter_combobox.pack()


filter_combobox.bind("<<ComboboxSelected>>",
                    lambda event: apply_filter(filter_combobox.get()))

我想通过将第二个块更改为

来成为 CTkComboBox
filter_combobox = customtkinter.CTkComboBox(left_frame, 
                                values=["Black and White", "Blur",
                                        "Emboss", "Sharpen", "Smooth"])

打印没有问题,但选项(模糊、锐化、平滑、浮雕)不再起作用!

我安装了所有必需的库,并且在我将这部分转换为 Customtkinter 之前,整个代码运行良好

python tkinter combobox ttk customtkinter
1个回答
0
投票

您应该使用

command
参数:

filter_combobox = customtkinter.CTkComboBox(left_frame, 
                                values=["Black and White", "Blur",
                                        "Emboss", "Sharpen", "Smooth"],
                                command=apply_filter)

该命令将选定的值传递给指定的函数。

这是指向 CTkComboBox 文档的

链接

© www.soinside.com 2019 - 2024. All rights reserved.