Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a widget's font style without knowing the widget's font family/size?

Is there a way to change a Tkinter widget's font style without knowing the widget's font family and font size?

Use case: We create our UI using standard Tkinter widgets (Label, Entry, Text, etc). While our application runs we may want to dynamically change the font style of these widgets to bold and/or italics using the .config() method. Unfortunately there appears to be no way to specify a font spec without specifying the font's family and size.

The following are examples of what we'd like to do, but neither of these examples work:

widget.config(font='bold') 

or

widget.config(font=( None, None, 'bold' )) 
like image 205
Malcolm Avatar asked Nov 01 '10 18:11

Malcolm


People also ask

Is font style and font-family Same?

With a font-family you set more than one font to the selected item that could be reverted to should the 1st font fail to load, the 2nd will take it's place and so on while with the font-style you set only one font to selected item with no back up font to revert to.

How do you change the font of an element font-family?

To change font type purely with HTML, use the CSS font-family property. Set it to the value you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a paragraph, heading, button, or span tag.

What is the difference between font size and font-family?

And a “font family” represents a collection of related fonts, such as bold and italic variations of the same “typeface” or “font”. In summary, in common terminology, typeface (aka font) means the design, font means the file containing the typeface, and font-family means collection of related fonts.

How do you change the font style in Python?

In your Python program, import tkinter. font as font, create font. Font() object with required options and assign the Font object to font option of Button. In this tutorial, we shall learn how to change the font-family, font size and font weight, with the help of well detailed example Python programs.


1 Answers

There's a much better way than using .config() to change your application font, especially if your goal is to change the font for a whole group of widgets (or all widgets).

One of the really great features of Tk is the notion of "named fonts". The beauty of named fonts is, if you update the font, all widgets that use that font will automatically get updated. So, configure your widgets once to use these custom fonts, then changing the attributes is trivial.

Here's a quick example:

# python 2 imports # import Tkinter as tk # import tkFont  # python 3 imports import tkinter as tk import tkinter.font as tkFont   class App:     def __init__(self):         root=tk.Tk()         # create a custom font         self.customFont = tkFont.Font(family="Helvetica", size=12)          # create a couple widgets that use that font         buttonframe = tk.Frame()         label = tk.Label(root, text="Hello, world", font=self.customFont)         text = tk.Text(root, width=20, height=2, font=self.customFont)         buttonframe.pack(side="top", fill="x")         label.pack()         text.pack()         text.insert("end","press +/- buttons to change\nfont size")          # create buttons to adjust the font         bigger = tk.Button(root, text="+", command=self.OnBigger)         smaller = tk.Button(root, text="-", command=self.OnSmaller)         bigger.pack(in_=buttonframe, side="left")         smaller.pack(in_=buttonframe, side="left")          root.mainloop()      def OnBigger(self):         '''Make the font 2 points bigger'''         size = self.customFont['size']         self.customFont.configure(size=size+2)      def OnSmaller(self):         '''Make the font 2 points smaller'''         size = self.customFont['size']         self.customFont.configure(size=size-2)  app=App() 

If you don't like that approach, or if you want to base your custom font on the default font, or if you're just changing one or two fonts to denote state, you can use font.actual to get the actual size of a font for a given widget. For example:

import Tkinter as tk import tkFont  root = tk.Tk() label = tk.Label(root, text="Hello, world") font = tkFont.Font(font=label['font']) print font.actual() 

When I run the above I get the following output:

{'family': 'Lucida Grande',   'weight': 'normal',   'slant': 'roman',   'overstrike': False,   'underline': False,   'size': 13} 
like image 55
Bryan Oakley Avatar answered Sep 23 '22 23:09

Bryan Oakley