Python 我是否可以将图像/标签放入Tkinter的选项框中?

Python 我是否可以将图像/标签放入Tkinter的选项框中?,python,image,tkinter,label,options,Python,Image,Tkinter,Label,Options,我不知道是否有可能实现这样的目标: [虽然选项菜单不直接支持它,但选项菜单只是一个菜单按钮和一个菜单。您可以在创建小部件后重复菜单并添加图像 下面是一个简单的例子: import tkinter as tk from tkinter import ttk root = tk.Tk() image1 = tk.PhotoImage(width=16); image1.put('red', to=(0,0,15,15)) image2 = tk.PhotoImage(width=16); ima

我不知道是否有可能实现这样的目标:


[虽然
选项菜单
不直接支持它,但
选项菜单
只是一个
菜单按钮
和一个
菜单
。您可以在创建小部件后重复菜单并添加图像

下面是一个简单的例子:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

image1 = tk.PhotoImage(width=16); image1.put('red', to=(0,0,15,15))
image2 = tk.PhotoImage(width=16); image2.put('green', to=(0,0,15,15))
image3 = tk.PhotoImage(width=16); image3.put('blue', to=(0,0,15,15))
images = {"red": image1, "green": image2, "blue": image3}

colorvar = tk.StringVar(value="red")
om = tk.OptionMenu(root, colorvar, "red", "green", "blue")
menu = om.nametowidget(om.menuname)
for label, image in images.items():
    print(f"configuring {label}")
    menu.entryconfigure(label, image=images[label], compound="left")

om.pack(padx=20, pady=20)

root.mainloop()

标签是指文本吗?您可以将文本放入选项中box@acw1668:我不认为这是真的。optionmenu只是一个菜单按钮和一个菜单,菜单项可以同时包含文本和图像。这可能需要一些额外的工作,但并非不可能。