How to Change a Widget's Font Style Without Knowing the Widget's Font Family/Size

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

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", which are implemented as objects in tkinter. 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 a font object, then you can update all widgets at once simply by changing the configuration of the font object.

Here's a quick example:

import tkinter as tk
import tkinter.font

class App:
def __init__(self):
root=tk.Tk()
# create a custom font
self.customFont = tkinter.font.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(side="top", fill="x")
text.pack(side="top", fill="both", expand=True)
text.insert("end","press +/- buttons to change\nfont size")

# create buttons to adjust the font
increase_font = tk.Button(root, text="+", command=self.increase_font)
decrease_font = tk.Button(root, text="-", command=self.decrease_font)

increase_font.pack(in_=buttonframe, side="left")
decrease_font.pack(in_=buttonframe, side="left")

root.mainloop()

def increase_font(self):
'''Make the font 2 points bigger'''
size = self.customFont['size']
self.customFont.configure(size=size+2)

def decrease_font(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 tkinter.font

root = tk.Tk()
label = tk.Label(root, text="Hello, world")
font = tkinter.font.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}

Change all children's font family in Flutter

You can wrap the widget whose descendants you want to change (e.g. MyWidget()) in a Theme Widget. For example:

Container(
child: Theme(
data: Theme.of(context).copyWith(
textTheme: TextTheme(<your changes here>),
),
child: MyWidget()
)
)

Changing font size for multiple labels in tkinter

Use ttk widgets. They have support for "themes" (aka "styles").

You can create a single style, and apply it to all the labels. (Example inspired by the documentation)

style = ttk.Style()
style.configure(
"BW.TLabel",
foreground="black",
background="white",
font="Helvetica",
fontsize=12
)

l1 = ttk.Label(text="Test", style="BW.TLabel")
l2 = ttk.Label(text="Test", style="BW.TLabel")

How to change the default font family in Flutter

You can change the default font family of your Flutter app by following the below steps:

1. Add your font files into your project folder. Say Project Folder > assets > fonts > hind.

2. Declare the font family with font files with style in your project's pubspec.yaml file as (An example):

Sample Image


  1. In the MaterialApp widget of your main class file, define the default font family as:

Sample Image

How do I change the text size in a Label widget? (tkinter)

Try passing width=200 as additional paramater when creating the Label.

This should work in creating label with specified width.

If you want to change it later, you can use:

label.config(width=200)

As you want to change the size of font itself you can try:

label.config(font=("Courier", 44))

How to change Font style in Html widget in Flutter?

Using flutter_html for richtext html.

Try this code to change the Html font style:

Html(
data: 'my text',
style: {
"body": Style(
fontSize: FontSize(18.0),
fontWeight: FontWeight.bold,
),
},
)

Flutter default font size

You should prefer composition over inheritance.

class Mono12Text extends StatelessWidget {
final String data;
final TextAlign align;
final TextStyle style;

Mono12Text(
this.data, {
this.align,
TextStyle style = const TextStyle(),
}) : style = style.copyWith(
fontFamily: 'Monospace',
fontSize: 12,
);

@override
Widget build(BuildContext context) {
return Text(
data,
textAlign: align,
style: style,
);
}
}

Python 3 /Tkinter: changing the font type, size, and color of a pre-defined label

This line assigns None to self.leftLabel.

self.leftLabel = Label(self.leftFrame, textvariable=self.defaultLeftStringValue).pack()

You can't chain the geometry management methods and retain a reference to the widget. Do these steps separately. In your case you have no reference to the label instance so cannot set its properties later.

To illustrate:

x = tk.Label(root, text="ok").pack()
type(x)
<class 'NoneType'>
x = tk.Label(root, text="ok")
type(x)
<class 'tkinter.Label'>


Related Topics



Leave a reply



Submit