Tkinter Button anchor

Tkinter Button anchor sets the position of text in Button widget.

In this tutorial, we will learn how to use anchor option of Button() class with examples.

Example 1 – Anchor Values

Following code snippet provides the possible anchor values.

tkinter.Button(window_main, anchor=tkinter.N) #North
tkinter.Button(window_main, anchor=tkinter.NE) #North-East
tkinter.Button(window_main, anchor=tkinter.E) #East
tkinter.Button(window_main, anchor=tkinter.SE) #South-East
tkinter.Button(window_main, anchor=tkinter.S) #South
tkinter.Button(window_main, anchor=tkinter.SW) #South-West
tkinter.Button(window_main, anchor=tkinter.W) #West
tkinter.Button(window_main, anchor=tkinter.NW) #North-West
tkinter.Button(window_main, anchor=tkinter.CENTER) #Center
ADVERTISEMENT

Example 2 – Tkinter Button anchor

In the following program, we will set a value of North-East position for anchor option of Tkinter Button.

example.py – Python Program

import tkinter

window_main = tkinter.Tk(className='Tkinter - TutorialKart', )
window_main.geometry("400x200")

button_submit = tkinter.Button(window_main, text ="Submit", anchor=tkinter.NE)
button_submit.config(width=20, height=2)

button_submit.pack()
window_main.mainloop()

By default, button wraps the text. So, in the above program, the width and height of button are changed, just to demonstrate how the text positions in a bigger size button for a given anchor value.

Output

When you run the application, you will see the following window.

Tkinter Button

During the click on button, you will see the active foreground color set.

Tkinter Button Anchor

Conclusion

In this Python Tutorial, we learned about Tkinter Button option “activeforeground” with the help of example Python programs.