Michel Michaud
Michel Michaud

Reputation: 21

How to have single word in sentence to be bold in tkinter (python)

I'm trying to display first name and last name using tkinter in python but the last name must be in bold. Since the first name vary in length, I can't use two different label and could not figure out how to calculate the length of the first name to have the last name at the right location (following the first name). Right now the code is simple but the last name is not bold!

from tkinter import Tk, Label
root = Tk()
first_name = "John"
last_name = "Doe"
name = first_name + " " + last_name
Label(root, text=name).place(x=100, y=100)

Found similar issue on the internet but could not understand the code to make it work. I don't have too much experience on Python. Any help would be appreciated.

Upvotes: 0

Views: 77

Answers (2)

acw1668
acw1668

Reputation: 47085

You can create a custom Frame widget with two Label widgets inside:

class NameLabel(tk.Frame):
    def __init__(self, master, first_name, last_name, font=('', 16), **kwargs):
        frame_kwargs = {}
        for opt in ('bd', 'relief', 'padx', 'pady'):
            frame_kwargs[opt] = kwargs.pop(opt, None)
        frame_kwargs['bg'] = kwargs.get('bg', None)
        super().__init__(master, **frame_kwargs)
        self._first_name = tk.Label(self, text=first_name, font=font, **kwargs)
        self._first_name.pack(side='left')
        self._last_name = tk.Label(self, text=last_name, font=font+('bold',), **kwargs)
        self._last_name.pack(side='left')

Then use this custom widget to show the first and last name:

NameLabel(
    root,
    first_name=first_name,
    last_name=last_name,
    font=('Times', 24),
    fg='red', bg='yellow',
    bd=1, relief='solid',
    padx=10, pady=5,
).place(x=100, y=100)

Result:

enter image description here

Upvotes: 2

JRiggles
JRiggles

Reputation: 6810

You could definitely do this with two different Label widgets - you'd just have to use a geometry manager other than place (e.g. pack) to put the labels next to each other.

from tkinter import Tk, Label


root = Tk()
first_name = Label(root, text='John Jacob Jingleheimer', font=('Arial', 10))
last_name = Label(root, text='Schmidt', font=('Arial', 10, 'bold'))
first_name.pack(side='left')
last_name.pack(side='left')
root.mainloop()  # you'll also need to start the main event loop!

Another slightly more complex option is to use so-called "tags" in a Text widget, but you'll have to make the widget read-only if you don't want the user to modify its contents.

from tkinter import Tk, Text


root = Tk()
first_name = 'John Jacob Jingleheimer'
last_name = 'Schmidt'

# create a Text widget with your desired font
text_field = Text(root, font=('Arial', 10))
# insert the text you want
text_field.insert('insert', f'{first_name} {last_name}')
# set up a tag to make everything after the first name bold
text_field.tag_add('bold', f'1.{len(first_name)}', 'end')
text_field.tag_configure('bold', font=('Arial', 10, 'bold'))
# make the text field read-only - NOTE: you'll need to set the state to
# 'normal' if you want to modify the text again later
text_field.config(state='disabled')
text_field.pack()

root.mainloop()

NB: the name 'bold' used as the first argument to tag_add and tag_configure is just that: the name of the tag; you can call it whatever you like!

Upvotes: 2

Related Questions