【问题标题】:Can I possibly put an image/label into an option box in Tkinter?我可以将图像/标签放入 Tkinter 的选项框中吗?
【发布时间】:2021-07-23 20:34:24
【问题描述】:

我不知道是否有可能实现这样的事情:

[????日出↕] 一世 (最好是调整大小为标签的图像)

【问题讨论】:

  • 标签是指文字吗?您可以在选项框中输入文本
  • @acw1668:我认为这不是真的。一个选项菜单就是一个菜单按钮和一个菜单,一个菜单项可以有文本和图像。这可能需要一些额外的工作,但这并非不可能。

标签: python image tkinter label options


【解决方案1】:

OptionMenu 不直接支持它,但OptionMenu 只是一个Menubutton 和一个Menu。您可以在创建小部件后遍历菜单并添加图像。

这是一个简单的例子:

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()

【讨论】:

    猜你喜欢
    • 2022-08-06
    • 1970-01-01
    • 2020-12-12
    • 2021-11-29
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 2012-04-24
    相关资源
    最近更新 更多