sg_calculator_tkinter_fonts.py
sg_calculator_tkinter_fonts.py adds font support, shows Scrollbar/layout error when the button font gets too big.
tkinter uses a default font for text on buttons but you can change it if you wish, as this sample code shows. While I was playing with this I noticed an unexpected error in my scrollbar layout, which I’ll describe below (and which I still don’t know how to fix). I first noticed a layout problem caused by fonts last fall when someone ran the RPSLS game on a Mac and the text was out of alignment, caused by differing default system fonts. Specifying non-default fonts fixed it. So I’ll demo how to change them here with the Button widget. I added the statement “font = myfont” to the Button definitions starting at line 140.
Lines 130-134 define a few font variables by name, size and style, then one of those is assigned to ‘myfont’ in line 136. So just change the variable assigned to myfont to change the button font. This is where I saw the layout problem. On line 136 switch to ‘helv16b’ and run it. Oops. The Display: text box stays the same size but the scrollbar extends to match the larger buttons. This is caused by the ‘rowspan = 4’ config in line 182, which tells the scrollbar to line up with 4 rows (the last three buttons plus the blank space Label), rows that got larger with the new font size. Switch the font back to v8 and switch the height of the text box from 6 to 4 (line 178, “height = 4”) and the scrollbar is again too big.
Change the text box width to a larger value like 12 with a ‘normal’ font (default or v8) and the buttons space apart, which looks bad.
Suggested student project: can you find the magic configuration that forces the scrollbar to stay within the text box instead of realigning independently? If you can figure this out I’ll be impressed (I don’t know the answer but I suspect you can do it somehow ... kudos to the first to do it right).
In the next (final) program for the calculator I’ll show my workaround for the scrollbar problem by changing the layout options instead of changing the scrollbar configuration, since I was too dumb to figure out how to bind the scrollbar to the text box properly.
http://www.tkdocs.com/tutorial/fonts.html good tutorial on tkinter fonts
http://www.tutorialspoint.com/python/tk_fonts.htm
http://www.pythonware.com/library/tkinter/introduction/x444-fonts.htm