0

I am new to software development. I have been busy trying to code a button with different multiline text and different fonts.

This is the closest I have got, with the help of Creating a custom widget in tkinter:

class FrameLabels(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.configure(width=100, height=50, bg='white')

        self.label = Label(self, text="fe", fg='black', bg='white',
                           font=("Calibri", 14, 'bold'))
        self.label.pack()

        self.label = Label(self, text="fef", fg='black', bg='white',
                            font=("Calibri", 12))
        self.label.pack()


class CustomButton(Button):
    def __init__(self, parent):
        Button.__init__(self, parent)
        self.button = FrameLabels(self)
        self.button.pack() 

This is how I am calling the widget in another class:

a = CustomButton(self)
a.grid(row=0, column=0)

With this code, I am indeed getting a frame with different lines of text "within" a button. The problem is that only the border of the custom widget functions as a button. Moreover, it still makes a weird effect when I click on it.

Would someone please help me? I would really appreciate it.

Paco
  • 3
  • 5
  • When creating custom widgets, most (if not all) of the time, you should be inheriting from `tk.Frame`. Right now you are inheriting from `tk.Button`. So when you call `self.button.pack()`, it messes up the `CustomButton`. – TheLizzard Sep 15 '22 at 18:22
  • Could you please give me a short example of how could I get the Custom widget that I want inheriting the second class from a Frame? – Paco Sep 19 '22 at 10:46
  • You have a good example linked in your question. Also if you want a multi-line button, look at [this](https://stackoverflow.com/q/26132192/11106801) – TheLizzard Sep 19 '22 at 10:48
  • Hi TheLizzard, thanks a lot for your answers. The income for the Button's text is coming from different values of a database. That is why I am trying to use labels inside a frame (with a button functionality). I can't just type the button's text I want, using \n to split the text into lines. Each button, with its frame, will be one entry/row in the database table and each label will have each value/field of this entry. I also want to have different fonts. The example of the link I added in my post is not about a frame with button functionality. – Paco Sep 19 '22 at 12:10

0 Answers0