Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use subscripts in Tkinter Label?

Tags:

python

tkinter

How can I use subscripts in a Tkinter Label?

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

like image 211
user1598203 Avatar asked Oct 25 '25 22:10

user1598203


1 Answers

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.

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.

For example:

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()
like image 189
Bryan Oakley Avatar answered Oct 28 '25 04:10

Bryan Oakley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!