0

Can anyone help me to align the button to the bottom of the screen in Tkinter. I am following a youtube tutorial, and in their code they write what i have written, but it aligns the button to the bottom. I am Using python 3.7 on a mac

from tkinter import *

root = Tk() #makes a blank popup, under the variable name 'root'

topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)

button1 = Button(topFrame, text='Button 1', fg='red')
button2 = Button(topFrame, text='Button 2', fg='blue')
button3 = Button(topFrame, text='Button 3', fg='green')
button4 = Button(topFrame, text='Button 4', fg='pink')

button1.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
button4.pack(side=BOTTOM)

root.mainloop() #loops the program forever until its closed
Sam Penny
  • 1
  • 1
  • 1
  • 1
  • What's the result you're expecting? I'm getting four buttons horizontally aligned, from left to right. Do you want them to be vertically aligned? – Right leg Feb 10 '18 at 18:06
  • Is the button you want at the bottom supposed to be in `bottomFrame`, or is it supposed to be above or below `bottomFrame`? – Bryan Oakley Feb 10 '18 at 21:22

3 Answers3

2

I think you should add button4 to bottomFrame:

button4 = Button(bottomFrame, text='Button 4', fg='pink')
davidedb
  • 867
  • 5
  • 12
2

This may be considered impractical in most cases but one way would be to simply swap the line of:

button1.pack(side=LEFT)

with:

button4.pack(side=BOTTOM)

Which would make it so that button4 fills the first empty space, as opposed to the last, in the "cavity" further explained here.

Nae
  • 14,209
  • 7
  • 52
  • 79
1

I would suggest you use grid() rather than pack(), it allows positioning to be much more controllable.

The grid() method creates a sort of table with rows and columns that allows you to position your elements.

Here is the sort of layout I have in mind:

 -----
|A|B|C|
 -----
| |D| |
 -----
  • A: Row 1, column 0
  • B: Row 1, column 1
  • C: Row 1, column 2
  • D: Row 2, column 1

If this is not what you want your elements to look like please edit your post or comment and I will edit my answer accordingly.


Bearing that in mind we can change .pack() to this method:

from tkinter import *

root = Tk() #makes a blank popup, under the variable name 'root'

topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)

button1 = Button(topFrame, text='Button 1', fg='red')
button2 = Button(topFrame, text='Button 2', fg='blue')
button3 = Button(topFrame, text='Button 3', fg='green')
button4 = Button(topFrame, text='Button 4', fg='pink')

button1.grid(column=0, row = 1)
button2.grid(column=1, row = 1)
button3.grid(column=2, row = 1)
button4.grid(column=1, row = 2)

root.mainloop() #loops the program forever until its closed

pack() and grid() cannot be used together, you either have to use one or the other.

I also advise against from tkinter import * this is unsafe and may override functions and will most likely at some point cause you problems.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • 2
    Independently of the preferred geometry manager,, I keep thinking that button4 should be added to bottomFrame. – davidedb Feb 10 '18 at 18:27
  • 1
    How is `from tkinter import *` not thread-safe? – Right leg Feb 10 '18 at 18:39
  • 1
    @davidedb You are quite right, that fixes the issue. I was just listing another way to go around solving the problem. – Xantium Feb 10 '18 at 18:44
  • @Rightleg My mistake `from tkinter import *` is unsafe and may overwrite functions, tkinter is not thread safe. https://stackoverflow.com/questions/14168346/python-tkinter-with-threading-causing-crash – Xantium Feb 10 '18 at 18:49