Reputation: 649
I am trying to create the interface for my program using tkinter library.
My interface should appears like this
BUTTON1
BUTTON2
BUTTON3
B.pack(side = "left", padx = 00, pady = 0)
C.pack(side = "left", padx = 100, pady = 100)
D.pack(side = "left", padx = 50, pady = 120)
I have already tried to change the parameters for padx and pady, But still it appears in the same line. I am using ubuntu 13.04 OS
How to set the parameters to get the desired format
Thanks in advance..:)
Upvotes: 1
Views: 160
Reputation:
You can fix your problem in three simple steps. First, remove all of the padx
's because they throw the buttons out of alignment. Second, use anchor = "w"
to make the buttons left justified. Third, set side
equal to "top"
so that the buttons are placed vertically and not horizontally. (however, since side
defaults to "top", you can just remove it altogether). Below is the fixed script:
from Tkinter import Tk, Button
root = Tk()
B = Button(root, text="BUTTON1")
C = Button(root, text="BUTTON2")
D = Button(root, text="BUTTON3")
B.pack(anchor = "w", pady = 0)
C.pack(anchor = "w", pady = 100)
D.pack(anchor = "w", pady = 120)
root.mainloop()
Another alternative is to use the grid
method instead of pack
:
from Tkinter import Tk, Button, W
root = Tk()
B = Button(root, text="BUTTON1")
C = Button(root, text="BUTTON2")
D = Button(root, text="BUTTON3")
# 'row' sets the row number that the widget is placed on
# 'sticky' is the same as 'anchor'
B.grid(row=0, pady=0, sticky=W)
C.grid(row=1, pady=100, sticky=W)
D.grid(row=2, pady=120, sticky=W)
root.mainloop()
While I personally prefer grid
over pack
, the decision is really just a matter of preference.
Upvotes: 1