2

How would I be able to multiple fonts into a widget?

I found out this may work.

b=Button(frame, text="Big bold text\n" "Small text")

I have tried many things however i can't get my code to work. Simply put, I want a big bold text and a small text underneath each other.

Thanks for any help and advice.

Dennis
  • 45
  • 9

2 Answers2

0

Neither the Tk button widget nor the ttk button widget support the use of multiple fonts for the text element. However, you can construct something using the Canvas widget to get two separate text elements within a single widget. This answer about making vertical text buttons provides a similar example that would be a good starting point.

patthoyts
  • 32,320
  • 3
  • 62
  • 93
0

I am aware that this thread is quite old at the time of writing but wanted this exact problem but with a Label instead. To solve this, please try the following code (hopefully it is very self-explanatory to use):

from tkinter import *
from tkinter.font import *

def example_method (): print ("Click!")

class Button2:

    """
Allows multiple fonts in a very simple button.
Only supports 'master', 'text' and 'command' keywords
    ('master' is compulsory)
Fonts are delared in < > with the following options:
    BOLD = Make the text bold
    ITALIC = Make the text italic
    STRIKEOUT = Strike through the text
    UNDERLINE = Underlines the text
    an integer = the text size
    any other keyword is assumed to be the text family
    For the default text style, leave the < > empty
NOTE: Only supports the grid method due to internal handelling
    """

    def __init__ (self, master, text = "<>", command = None):

        self.f, self.command = Frame (root, relief = RAISED, borderwidth = 3), command
        self.f.bind ("<Button-1>", lambda event: self.__click ())
        self.f.bind ("<ButtonRelease-1>", lambda event: self.__release ())

        sections = [i.split (">") for i in text.split ("<") [1 : ]]

        row, column = 0, 0
        for section in sections:

            font_decomp, kw = section [0].split ("_"), {}
            for keyword in font_decomp:
                if keyword == "STRIKEOUT": kw ["overstrike"] = True
                elif keyword == "BOLD": kw ["weight"] = BOLD
                elif keyword == "ITALIC": kw ["slant"] = ITALIC
                elif keyword == "UNDERLINE": kw ["underline"] = True
                try: kw ["size"] = int (keyword)
                except: kw ["family"] = keyword

            temp_font = Font (**kw)
            l = Label (self.f, text = section [1].replace ("\n", ""), font = temp_font)
            l.grid (row = row, column = column)
            l.bind ("<Button-1>", lambda event: self.__click ())
            l.bind ("<ButtonRelease-1>", lambda event: self.__release ())

            if section [1].count ("\n") >= 1:
                column = -1
                for i in range (section [1].count ("\n") - 1):
                    l = Label (self.f, text = "", font = temp_font)
                    l.grid (row = row, column = column)
                    l.bind ("<Button-1>", lambda event: self.__click ())
                    l.bind ("<ButtonRelease-1>", lambda event: self.__release ())
                    row += 1
                row += 1
            column += 1

    def __click (self):
        self.f.config (relief = SUNKEN)
        if self.command: self.command ()

    def __release (self): self.f.config (relief = RAISED)

    def grid (self, **kw): self.f.grid (**kw)

root = Tk ()
root.title ("Multiple fonts")
Button2 (root, text = "<30_BOLD>Big bold text\n<10>Small text", command = example_method).grid ()
root.mainloop ()
Minion Jim
  • 1,239
  • 13
  • 30