如何在 Tkinter 标签中使用下标? [英] How to use subscripts in Tkinter Label?

查看:49
本文介绍了如何在 Tkinter 标签中使用下标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Tkinter 标签中使用下标?

How can I use subscripts in a Tkinter Label?

我发现了很多帖子,比如 这个,但这对我没有帮助......

I found a lot of posts like this, but that does not help me...

推荐答案

对于丰富的格式,请使用小型文本小部件而不是标签.然后,您可以向信息添加各种格式.例如,您可以在文本标记上使用 offset 属性来创建上标和下标.

For rich formatting, use a small text widget rather than a label. You then have the ability to add all kinds of formatting to the information. You can, for example, use the offset attribute on a text tag to create superscripts and subscripts.

只需在配置小部件后将状态设置为 disabled,就所有意图和目的而言,它看起来就像一个标签.主要区别在于您必须手动设置大小,因为文本小部件不会像标签那样扩展以适应其内容.

Just set state to disabled after configuring the widget, and for all intents and purposes it will look like a label. The main difference is that you have to manually set the size since a text widget won't expand to fit its contents like a label does.

例如:

import Tkinter as tk
class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        l = tk.Text(self, width=5, height=2, borderwidth=0, 
                    background=self.cget("background"))
        l.tag_configure("subscript", offset=-4)
        l.insert("insert", "H", "", "2", "subscript", "O")
        l.configure(state="disabled")
        l.pack(side="top")

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

这篇关于如何在 Tkinter 标签中使用下标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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