1

So I have two frames, a centered text frame and a toolbar with buttons in it. I want the toolbar to be on top side, so I tried self.toolbar.pack(side='top', pady=60) but it doesn't seem to be enough.

What happens is that the buttons, who are slaves of the toolbar frame, seem to be dictating their own position : if I pack one left, it goes on the left side of the entire application. Instead, I would like to be able to place once my toolbar frame, and then pack my buttons, side by side, so using something like the side attribute that currently change their global position.

How can I achieve this? Is my OOP approach bad written?

The whole block:

import tkinter as tk


class ToolbarButton(tk.Button):

    def __init__(self, master, text, pixelref, *args, **kw):
        super(ToolbarButton, self).__init__()
        self.master = master
        super(ToolbarButton, self).configure(text=text, image=pixelref, height=20, width=20, compound='center')


class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        # Textframe
        self.text_frame = tk.Frame(root, width=600, height=790)
        self.text_frame.pack_propagate(False)
        self.text_widg = tk.Text(self.text_frame, width=1, height=1)
        self.text_widg.pack(expand=True, fill='both')

        # Toolbar
        self.toolbar = tk.Frame(root)
        self.pixel = tk.PhotoImage(width=1, height=1)

        self.bold_button = ToolbarButton(self.toolbar, 'B', self.pixel)
        self.bold_button.pack(side='left', padx=4)
        self.italic_button = ToolbarButton(self.toolbar, 'I', self.pixel)
        self.italic_button.pack(side='left', padx=4)
        self.underline_button = ToolbarButton(self.toolbar, 'U', self.pixel)
        self.underline_button.pack(side='left', padx=4)

        # Packing
        self.toolbar.pack(side='top', pady=60)
        self.text_frame.pack(expand=True)


if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()
atrefeu
  • 178
  • 11
  • 1
    Please create a [mcve]. We can't run your code as posted. Your description sounds like you are doing everything right, so we need to be able to reproduce your problem. – Bryan Oakley Mar 06 '21 at 16:43
  • @BryanOakley added! – atrefeu Mar 06 '21 at 17:49
  • That code isn't minimal - code to set the state is irrelevant to the question, as is defining multiple custom fonts. Also, please do not link to code on another site. – Bryan Oakley Mar 06 '21 at 18:32
  • I didn't want to drown the main question with code. I'll try to replace the current code with a new minimal one – atrefeu Mar 06 '21 at 18:35
  • _"I didn't want to drown the main question with code."_ - that's why we request a _minimal_ example. You can easily remove 15-20 lines of that code. – Bryan Oakley Mar 06 '21 at 18:38

1 Answers1

0

Someone explained me the issue: the ToolbarButton class wasn't instantiating properly. A way to correct this - and improve the syntax too:

class ToolbarButton(tk.Button):
    def __init__(self, master, text, pixelref, *args, **kw):
        super().__init__(master)
        self.configure(text=text, image=pixelref, height=20, width=20, compound='center')
atrefeu
  • 178
  • 11