如何在Tkinter中的一个按钮中显示图像+文本 [英] How to have image + text in one button in Tkinter

查看:750
本文介绍了如何在Tkinter中的一个按钮中显示图像+文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个按钮,其中包含一个与左边对齐的图像和一个与右边对齐的文本。我只想通过参数text更改文本,而不是通过修改整个图像。这有可能吗?

I´m trying to create a button, which will include an image aligned to the left and text aligned to the right. I just want to change text by parameter "text", not by modifying whole image. Is this possible somehow?

这是一个简单的例子,我的意思。

Here´s a simple example, what I mean.

http://img651.imageshack.us/img651/3776/previewrv.png

希望我解释得很好

谢谢

推荐答案

查看标签的化合物选项。它允许您指定标签与文本的关系(顶部,底部,左侧,右侧,无)。

Look at the compound option to the label. It lets you specify the relationship of the label to the text (top, bottom, left, right, none).

例如:

import Tkinter as tk

class View(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.image = tk.PhotoImage(file="somefile.gif")
        b = tk.Button(self, text="Hello, world", image=self.image, compound="left")
        b.pack(side="top")

if __name__ == "__main__":
    root = tk.Tk()
    view = View(root)
    view.pack(side="top", fill="both", expand=True)
    root.mainloop()

这篇关于如何在Tkinter中的一个按钮中显示图像+文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆