2

I have a snippet where I generate a string that includes an original number (original_number) and its corresponding Roman numeral (roman_number). I want to format this string so that the original number and the Roman numeral are displayed in bold. Here's the relevant part of the code:

explanation_text = f"{original_number} is {roman_number} in Roman numerals:\n\n" + " + ".join(explanation)

How can I modify this code to ensure that both original_number and roman_number are displayed in bold font when displayed?

I tried using \033[1m for bold formatting However, it didn't work. The text was not displayed in bold font.

3
  • 5
    Short answer: you can't. Longer answer: it depends on how you're going to display this string. Are you wanting to print it on the console? Display in a tkinter label? A tkinter text widget? Something else? Commented May 24, 2024 at 17:37
  • 2
    To expand on @BryanOakley's comment: if your intention is to do this in tkinter (which I assume, given the tag on this question), it would likely involve setting the font on a tkinter.Label widget, or appropriately styling a ttk.Label widget.
    – JRiggles
    Commented May 24, 2024 at 17:44
  • I'm displaying the text in tkinter text widget.
    – aarondn
    Commented May 25, 2024 at 18:48

3 Answers 3

2

You can use tag_configure() and define 'bold':

import tkinter as tk

num = 2024
roman = "MMXXIV"
explanation = ["some text", "some other text"]


root = tk.Tk()

T = tk.Text(root, wrap='word', width=100, height=20)
T.pack(padx=50, pady=50)

T.tag_configure('bold', font=('Arial', 20, 'bold'))
T.insert('1.0', f"{num} is ", 'bold')
T.insert('end', f"{roman} ", 'bold')
T.insert('end', " + ".join(explanation))

root.mainloop()


Prints

enter image description here

2
  • 1
    Just some extra information: label = tkinter.Label(Window, text="This is Bold, Italic, Underlined and Overstriked Arial Font at 8 pixels.", font=("Arial", 8, "bold italic underlined overstriked")) There are two options for weight of the font, bold and normal and for slant, roman and italic. Commented May 25, 2024 at 6:05
  • 1
    Thanks a lot it worked, i used 'regular' for the customization of the rest.
    – aarondn
    Commented May 25, 2024 at 18:50
1

If you are doing html output you can do this ( might work too):

explanation_text = f"<b>{original_number}</b> is <b>{roman_number}</b> in Roman numerals:\n\n" + " + ".join(explanation)

If you are in the terminal you can try this:

explanation_text = f"\033[1m{original_number}\033[0m is \033[1m{roman_number}\033[0m in Roman numerals:\n\n" + " + ".join(explanation)

I know you already tried this but it might be worth copying and pasting to make sure you didn't make a simple syntax error. Here is another page on this topic: How does one output bold text in Bash?

It might be worthwile to clarify where you are trying to print the text (Bash, Powershell, HTML, etc.). Good luck!

1
  • I am trying to print the text within Tkinter, I'm using the Text widget. \033[1m doesn't work i do not know why. someone said it doesn't work on the last python version. but i tried tag_configure() and it worked, thanks.
    – aarondn
    Commented May 25, 2024 at 18:55
0

Make sure to use the end termination '\033[0m' with what you tried like so

print('\033[1m' + 'Hello, World!' + '\033[0m')
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.