tkinter - 如何将此按钮放在中间的底部?

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

我想将按钮 boutonexcel 放在 window 的底部,中间(两个框架下方)。我尝试了很多 side=...anchor=... 的组合,但都没有成功。

怎么才能随意摆放呢?

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

好的,我自己找到了解决方案。

下面是带有新行注释的代码。为了更好的可读性,我在评论中加上了“解决方案:”。

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

在这种特定情况下,如果您在任何其他小部件之前对按钮调用 pack,并且将边设置为“底部”,它将在底部居中。

这是因为 pack 的调用顺序很重要。打包器通过分配可用 space 的整个一侧并将小部件放在该侧来工作。默认侧是“顶部”,但将其设置为“底部”会将小部件移动到底部。

一旦此按钮位于底部,该 space 将无法用于任何其他小部件。随后打包的任何其他小部件必须出现在它上面,因为那是未分配的 space 所在的位置。

此外,打包程序的默认设置是将小部件放置在为其分配的 space 的中心。因此,除非您另外指定,否则按钮将位于 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)
...