Caleb Heath
Caleb Heath

Reputation: 27

vertical padding between buttons in a column

When I pad the widgets they all just move together on the same frame in 1 big block. I need to be able to separate them and add the padding between each widget on the same frame. This is part of my code I tried:

def CreateDisplay(self):

    self.mainwindow = tk.Tk()
    self.mainwindow.geometry("800x600")
    self.mainwindow.wm_title(self.constants.NAME)
    self.mainwindow.resizable(width=False, height=False)

    self.frame_main = tk.Frame(master = self.mainwindow)
    self.frame_title = tk.Frame(master = self.frame_main)
    self.frame_test = tk.Frame(master = self.frame_main)
    self.frame_recentscore = tk.Frame(master = self.frame_main)
    self.frame_help = tk.Frame(master = self.frame_main)
    self.frame_exit = tk.Frame(master = self.frame_main)

    self.frame_main.pack(pady=15)
    self.frame_title.pack(anchor="c")
    self.frame_test.pack(pady=15)
    self.frame_recentscore.pack(pady=15)
    self.frame_help.pack(pady=15)
    self.frame_exit.pack(pady=15)

    self.label_title = tk.Label(self.frame_title, font=("purisa", 20), text="Business Studies Finance Revison:")

    self.label_title.pack()

    self.button_test = tk.Button(self.mainwindow, padx=54, pady=3, font=("Purisa",15), text="Take a Test", command=self.parent.test).pack()
    self.button_recentscore = tk.Button(self.mainwindow, font=("Purisa",15), text="View My Recent Scores", command=self.parent.recentscores).pack()
    self.button_help = tk.Button(self.mainwindow, padx=88, pady=3, font=("Purisa",15), text="Help", command=self.parent.help_mainwindow).pack()
    self.button_exit = tk.Button(self.mainwindow, padx=91, pady=3, font=("Purisa",15), text="Exit", command=self.parent.Exit).pack()

Everything works other than the padding between the widgets. I have all the running files of this in a separate file as its a big project. The padding in the widgets definition is just to shape them not move them on the frame. How can we add vertical padding between the buttons in a column?

Upvotes: 2

Views: 14523

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385970

It appears you misunderstand how padding works. When you apply a padx or pady value to a widget, that padding only affects that one widget. For example, if you specify a pady value of 100 for the help button, only the help button is affected. If you specify pady value for a frame, that only affects how that frame is padded with respect to its parent.

As an example, if you want a padding of 20 pixels for every button, you must specify a pady value of 20 pixels for each and every button. If you want the buttons as a group to have padding around the group, you want to specify the padding for the frame.

Upvotes: 2

Honest Abe
Honest Abe

Reputation: 8758

You want to use:

x = tkinter.Button(...)
x.pack(pady=8)

By the way x = None when you do:

x = tkinter.Button(...).pack()

It's a classic beginners mistake.

Upvotes: 5

Related Questions