Home > Software engineering >  The code for button.pack(anchor="center") does not work in tkinter
The code for button.pack(anchor="center") does not work in tkinter

Time:05-23

The cllxk.pack(anchor="center") code I wrote in Python tkinter does not work and the button remains below.

cllxk = Button(root, text="Caps Lock Spam", width=14, command=clocks)
cllxk.pack(anchor="n")

CodePudding user response:

anchor specified how the widget is aligned in the space that has been allocated for it. The option side determines the space that is allocated. Since you didn't specify the side attribute, it defaults to "top". That means that the packer will allocate the top portion of the unused space, and places the widget along that side.

For the canonical description of how pack works, see The Packer Algorightm in the official tcl/tk documentation.

CodePudding user response:

You should not do button.pack(anchor="center"). Instead, what you should do is this:

button.pack(anchor=CENTER)

This is cause CENTER is a tkinter constant. This goes the same for

NW   N   NE
W  CENTER E
SW   S   SE
  • Related