How can I use subscripts in a Tkinter Label?
I found a lot of posts like this, but that does not help me...
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With