Friday, January 1, 2016

Python Tkinter Label widget set font size and bold without setting font

For user feedback, some kind of text in a frame that can be updated to show status is essential. It turns out that the Label widget is set up to do this naitively. Just define a text variable type and give the name of that variable during the setup of the Label widget and any changes to the text variable automatically show up in the widget. Pretty basic. The second-to-last example in this amazing tutorial demonstrate this with a slider that returns values and a label which is set up to dynamically display whatever the value of the slider is:

http://zetcode.com/gui/tkinter/widgets/

Basically, this entire site's tutorial is incredible and finally something pertinent to what I am doing:

http://zetcode.com/gui/tkinter/

The thing that I found out about which is more interesting to me is how to format the font in the Label widget. I personally didn't care to change the font itself from whatever was the system's default, but I wanted to make it bigger and make it bold. It turns out that this is also no problem; for the "font" specification you just omit specifying the font itself and just specify the size and weight, as in these links:

http://stackoverflow.com/questions/4072150/how-to-change-a-widgets-font-style-without-knowing-the-widgets-font-family-siz

http://effbot.org/tkinterbook/label.htm

So, the code that I made looks like this:

self.var = StringVar()
self.label = Label(self, text=0, textvariable=self.var, font="-size 20 -weight bold")