I'm trying to create different frames and switch/destroy them so that you can move between windows like you would in a normal iOS app.
To do so, I need to place the widgets (components) in frames (containers).
However, when I try to add a button to the frame it doesn't pack it to the right side.
Here is my code: from tkinter import *
root=Tk()
root.geometry('500x500')
root.title('Good morning :)')
frame1=Frame(root,width=500,height=500,bg='green')
frame1.pack()
button1=Button(frame1,text='Hello')
button1.pack(side='bottom')
The button in the tkinter module can be placed or move to any position in two ways: By using the place() method. And by using the pack() method.
You need to expand the Frame to fill the entire top-level window, and you need to tell the Button to pack on side='right' instead of side='bottom' . And you need to run root. mainloop() at the end.
Pack() has three options you can use: side, fill, expand. The side option aligns buttons horizontally. The fill option aligns buttons vertically. The expand option takes up the full height of the frame.
This method can be used on both windows and frames. The grid() method allows you to indicate the row and column positioning in its parameter list. Both row and column start from index 0. For example grid(row=1, column=2) specifies a position on the third column and second row of your frame or window.
You need to expand the Frame to fill the entire top-level window, and you need to tell the Button to pack on side='right'
instead of side='bottom'
.
And you need to run root.mainloop()
at the end.
from tkinter import *
root = Tk()
root.geometry('500x500')
root.title('Good morning :)')
frame1 = Frame(root, bg='green')
frame1.pack(expand=True, fill=BOTH)
button1 = Button(frame1, text='Hello')
button1.pack(side=RIGHT)
root.mainloop()
Also, you don't need the dimensions in the Frame statement, since it will expand to the full 500x500 stated in the geometry with the extra keyword arguments passed to the pack() function. By default, the Frame is only going to be big enough to hold the widgets inside it, so it will only be as big as the Button, unless you tell it to expand to the full size of the top-level root widget.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With