python - Python 的 Tkinter 可以在按钮小部件中同时显示文本和图像吗?

标签 python button tkinter toolbar

为了练习,我使用 Python 的 Tkinter 复制/重新创建另一个软件 GUI 的“选项”部分。这是我的模型 GUI:

enter image description here

此时,我的代码如下:

import tkinter as tk
import tkinter.ttk as ttk
import random
from PIL import Image, ImageTk

class Options(tk.Frame):

    def __init__(self, parent):

        random_numbers = lambda: [random.random() for i in range(4)]
        text_font_icon = r"D:\Dropbox\coding\python\experiments\6 - tkinter\icons\icon_mail.gif"

        labelframe_general     = tk.LabelFrame(parent, text="General")
        labelframe_general.pack()

        tk.Label(labelframe_general, text="Startup tab", justify=tk.LEFT).grid(column=0, row=0, sticky=tk.W)
        ttk.OptionMenu(labelframe_general, tk.StringVar(), random_numbers()[0], *random_numbers()).grid(column=1, row=0)

        tk.Label(labelframe_general, text="Episode display format", justify=tk.LEFT).grid(column=0, row=1, sticky=tk.W)
        ttk.OptionMenu(labelframe_general, tk.StringVar(), random_numbers()[0], *random_numbers()).grid(column=1, row=1)

        tk.Label(labelframe_general, text="Number of banner images to\ndownload per series", justify=tk.LEFT).grid(column=0, row=2, sticky=tk.W)
        ttk.OptionMenu(labelframe_general, tk.StringVar(), random_numbers()[0], *random_numbers()).grid(column=1, row=2)

        tk.Checkbutton(labelframe_general, text="Automatically scroll episode list to bottom", variable=tk.StringVar()).grid(column=0, row=3, columnspan=2, sticky=tk.W)
        tk.Checkbutton(labelframe_general, text='Move "The" to the end of series name', variable=tk.StringVar()).grid(column=0, row=4, columnspan=2, sticky=tk.W)
        tk.Checkbutton(labelframe_general, text="Close epCheck with the <Esc> key", variable=tk.StringVar()).grid(column=0, row=5, columnspan=2, sticky=tk.W)

        _photo = tk.PhotoImage(file=text_font_icon)
        b = ttk.Button(labelframe_general, text="List & Overview font (right-click for default", image=_photo)
        b.image = _photo
        b.grid(sticky=tk.NSEW, columnspan=2)


class Gui(tk.Tk):

    def __init__(self):

        super().__init__()
        Options(self)

        self.bind('<Escape>', lambda event: self.destroy())

gui = Gui()
gui.mainloop()[![enter image description here][2]][2]

上面的结果是:

enter image description here

我的问题仅在于 ttk.Button(所有其他小部件的行为都符合我的预期)。

该按钮仅显示图标,但不显示文本“列表和概述字体”。我的问题:

  1. 为什么使用图像时按钮上的文本消失了?

  2. Tkinter 能否将图标与按钮中的文本一起显示或在文本旁边显示(如上面的模型 GUI 中所示)?

  3. Tkinter“工具栏”或小部件是否能够将图标与文本一起显示,或者我是否只能选择纯文本按钮和纯图像按钮?

我尝试过 Menubutton 并测试了其他来源的代码,但没有成功。将不胜感激的帮助。

最佳答案

您需要为ttk.Buttoncompound选项指定一个值。

If you provide both image and text options, the compound option specifies the position of the image relative to the text. The value may be tk.TOP (image above text), tk.BOTTOM (image below text), tk.LEFT (image to the left of the text), or tk.RIGHT (image to the right of the text).

When you provide both image and text options but don't specify a compound option, the image will appear and the text will not.

关于python - Python 的 Tkinter 可以在按钮小部件中同时显示文本和图像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44391371/

相关文章:

python - 从大型 CSV (1GB) 中删除逗号

python - PyTorch 打印输出到 2dp

python - 如何在 tkinter python 中为变量指定不同的名称

python - 闪烁的 Tkinter 标签

python-3.x - 如何为 pygame 创建这个 tkinter 菜单栏

python - Ubuntu - 在 python : "ImportError: No module named mxnet" 中运行程序时出错

python - 无法在 macOS Catalina 上安装 psycopg2

Android - 每行调用动态监听器的 ListView 动态按钮

java - JavaFX 中的按钮不会响应

java - 测验应用程序,更改错误答案选择的错误和正确答案的按钮颜色,android