Skip to content
Advertisement

tkinter – How can I place this button on the bottom in the middle?

I would like to place the button boutonexcel at the bottom of the window, in the middle (below both frames). I tried many combination of side=... and anchor=... without success.

How could I place it as I wish?

import os
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox

fenetre_choice_of_mat=Tk()
fenetre_choice_of_mat.geometry("350x500")

class Checkbuttongroup:
    def __init__(self, fenetre1, text1):
        self.Checkbutton1 = IntVar()
        self.texte=text1
        self.fenetre12=fenetre1
        Button1 = Checkbutton(self.fenetre12, text = self.texte, 
            variable = self.Checkbutton1,
            onvalue = 1,
            offvalue = 0,
            height = 2,
            width = 20,
            anchor = "w")
        Button1.pack() 

cadre_choice_material_1=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_1.pack(side='left', anchor=N)
cadre_choice_material_2=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_2.pack(side='left',anchor=N)
grp_material_1=Checkbuttongroup(cadre_choice_material_1,"CW")
grp_material_2=Checkbuttongroup(cadre_choice_material_1,"CS")
grp_material_4=Checkbuttongroup(cadre_choice_material_1,"AE")
grp_material_6=Checkbuttongroup(cadre_choice_material_1,"CF")
grp_material_7=Checkbuttongroup(cadre_choice_material_1,"SE")
grp_material_3=Checkbuttongroup(cadre_choice_material_1,"RCC")
grp_material_5=Checkbuttongroup(cadre_choice_material_1,"CD")
grp_material_8=Checkbuttongroup(cadre_choice_material_2,"FS")
grp_material_9=Checkbuttongroup(cadre_choice_material_2,"COP")
grp_material_11=Checkbuttongroup(cadre_choice_material_2,"CR")
grp_material_12=Checkbuttongroup(cadre_choice_material_2,"AB")
grp_material_13=Checkbuttongroup(cadre_choice_material_2,"CT")
grp_material_14=Checkbuttongroup(cadre_choice_material_2,"LP")
grp_material_10=Checkbuttongroup(cadre_choice_material_2,"SD")

boutonexcel=Button(fenetre_choice_of_mat, text="TEST",width=15,height=1,bg="white",bd=5)
boutonexcel.pack()




fenetre_choice_of_mat.mainloop()

os.system("pause")

Advertisement

Answer

In this specific case, if you call pack on the button before any other widgets, and you set the side to “bottom”, it will be centered at the bottom.

This is because the order in which pack is called matters. The packer works by allocating an entire side of the available space, and putting the widget on that side. The default side is “top”, but setting it to “bottom” moves the widget to the bottom.

Once this button is on the bottom, that space is unavailable to any other widgets. Any other widgets that are subsequently packed must appear above it since that is where the unallocated space is.

Also, the default for the packer is to place the widget in the center of the space allocated for it. So, unless you specify otherwise the button will be in the middle of the bottom of the window.

boutonexcel=Button(fenetre_choice_of_mat, text="TEST",width=15,height=1,bg="white",bd=5)
boutonexcel.pack(side="bottom")

cadre_choice_material_1=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
...

screenshot

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement