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

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

问题描述

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

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

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

希望我解释得很好

谢谢

解决方案

查看标签的 compound 选项.它允许您指定标签与文本的关系(顶部、底部、左侧、右侧、无).

例如:

将 Tkinter 导入为 tk类视图(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")如果 __name__ == "__main__":根 = tk.Tk()视图 = 视图(根)view.pack(side="top", fill="both", expand=True)root.mainloop()

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

Hope I explained it well

Thank you

解决方案

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

For example:

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天全站免登陆