Reputation: 23
I am trying to to place 2 buttons in the bottom-right and bottom-left of the screen so they stick when the window gets resized however when I anchor them and pack them they end up like shown below. How do I fix this?
if newbuttonthere == False:
new_button = tk.Button(fightWindow, text="New", fg="black", command=newcharacter, font=("Courier", 20, "bold"))
new_button.pack(anchor=W, side=tk.BOTTOM)
new_button.configure(bg="grey")
if newbuttonthere == False:
newM_button = tk.Button(fightWindow, text="New Monster", fg="black", command=newmonster, font=("Courier", 20, "bold"))
newM_button.pack(anchor=E, side=tk.BOTTOM)
newM_button.configure(bg="grey")
Upvotes: 2
Views: 1137
Reputation: 43
Another option might be using the fill attribute, the following way:
from tkinter import Tk, Frame, Label
root = Tk()
root.geometry("400x400")
root.configure(background='#01324a')
# Label frame will contain your two other widgets
labelframe = Frame(root, bg='pink')
item1 = Label(labelframe, text='Left', bg='yellow')
item2 = Label(labelframe, text='Right', bg='green')
labelframe.pack(side='bottom', fill='x')
item1.pack(side='left')
item2.pack(side='right')
root.mainloop()
Fill will make the label expand horizontally, so it uses all the space in the assigned "row" rather than just the necessary.
I encourage you to check what would happen if you remove the fill attribute -> (pink will go behind the other widgets, as the space used will be the minimum needed)
Also, I suggest adding a frame in top of the bottom label, but it is not required.
Upvotes: 0
Reputation: 8037
@Joshua6014 .pack() will stack the widgets on the side you instruct them, or by default it will be tk.TOP. In other words, it creates new columns. So its impossible to achive what you like with .pack() as long as you intend to use the same master. Use another geometry manager, I would suggest you to use .grid() in this case.
Using different masters:
mstr_one = tk.Frame(root)
mstr_two = tk.Frame(root)
mstr_one.pack(side='left')
mstr_two.pack(side='right')
lab_one = tk.Label(mstr_one,text='right bottom')
lab_two = tk.Label(mstr_two,text='left bottom')
lab_one.pack(side='bottom',anchor='e')
lab_two.pack(side='bottom',anchor='w')
Using grid instead of pack:
lab_one = tk.Label(root,text='right bottom')
lab_two = tk.Label(root,text='left bottom')
lab_one.grid(column=0,row=0,sticky='e')
lab_two.grid(column=2,row=0,sticky='w')
root.grid_columnconfigure(1,weight=2)
Upvotes: 1