简体   繁体   中英

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")

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)
...

截屏

Ok I found by myself the solution.

Below is the code with the comments for the new lines. For a better readibility, I put "SOLUTION:" in the comment.

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() 

bottom_frame=Frame(fenetre_choice_of_mat) #-- SOLUTION: I created a frame to put at the bottom
bottom_frame.pack(side='bottom')

cadre_choice_material_1=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_1.pack(side='left')
cadre_choice_material_2=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_2.pack(side='left')
grp_material_1=Checkbuttongroup(cadre_choice_material_1,"CW")
grp_material_2=Checkbuttongroup(cadre_choice_material_1,"Car Sling")
grp_material_4=Checkbuttongroup(cadre_choice_material_1,"Accessories electric")
grp_material_6=Checkbuttongroup(cadre_choice_material_1,"CW Filler")
grp_material_7=Checkbuttongroup(cadre_choice_material_1,"Shaft equipment")
grp_material_3=Checkbuttongroup(cadre_choice_material_1,"Rail for car and CW")
grp_material_5=Checkbuttongroup(cadre_choice_material_1,"Car doors")
grp_material_8=Checkbuttongroup(cadre_choice_material_2,"Floor selector")
grp_material_9=Checkbuttongroup(cadre_choice_material_2,"COP")
grp_material_11=Checkbuttongroup(cadre_choice_material_2,"Car")
grp_material_12=Checkbuttongroup(cadre_choice_material_2,"Adjustable bracket")
grp_material_13=Checkbuttongroup(cadre_choice_material_2,"Control")
grp_material_14=Checkbuttongroup(cadre_choice_material_2,"LOP")
grp_material_10=Checkbuttongroup(cadre_choice_material_2,"Shaft doors")


boutonexcel=Button(bottom_frame, text="TEST",width=15,height=1,bg="white",bd=5) #-- SOLUTION: I put the button in the bottom_frame
boutonexcel.pack(side='bottom')




fenetre_choice_of_mat.mainloop()

os.system("pause")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM