Jeff Kint
Jeff Kint

Reputation: 27

How to center text in tkinter labels like Microsoft Word centered typing mode

I am trying to make a page to page system on tkinter, however having the text starting from the right onwards looks a little peculiar and I would prefer it if the text spread out evenly from the center of the label.
An example
This is some really cool text
<< Prev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . >> Next
To then become
. . . . . . . . . . . . .. This is some really cool text
<< Prev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . >> Next

The dots are there because Stackoverflow does not allow more than one space so Just ignore those. It should look just like Microsoft Word's centered writing
Here is the basic code I have, I tried using anchor = CENTER but that did not work as intended. Sorry if this is basic I'm new to Tkinter

from tkinter import *
introText = ["Some say","That it is","very important","to clean","your hands","Yababababa doooooooo"]
currentPage = 1
def forceSize(main, width, height):
    main.minsize(width, height)
    main.maxsize(width, height)
def storyGUI(textList):
    global currentPage
    textListLength = len(textList)
    def prevPage(main, textLabel):
        global currentPage
        if currentPage != 1:
            textLabel.config(text = introText[currentPage-2])
            main.title("Page "+str(currentPage-1))
            currentPage -= 1
    def nextPage(main, textLabel):
        global currentPage
        if currentPage != textListLength:
            textLabel.config(text = introText[currentPage])
            main.title("Page "+str(currentPage+1))
            currentPage += 1
    storyTK = Tk()
    forceSize(storyTK, 400, 100)
    storyTK.title("Page 1")
    textLabel = Label(storyTK, text = introText[0], font = ("Calibri", 13), anchor = CENTER)
    prevButton = Button(storyTK, text = "<< Prev", command = lambda: prevPage(storyTK, textLabel))
    nextButton = Button(storyTK, text = "Next >>", command = lambda: nextPage(storyTK, textLabel))
    textLabel.place(x = 160, y = 15)
    prevButton.place(x = 30, y = 60)
    nextButton.place(x = 310, y = 60)
    storyTK.mainloop()
#def intro():
storyGUI(introText)

Upvotes: 1

Views: 103

Answers (1)

probat
probat

Reputation: 1532

By default text for a tk label is centered inside the label so the issue here was that the label itself was not being centered.

The origin point for place is the upper most left corner x=0, y=0. Using textLabel.place(relx=0.5, y=15, anchor='center') centers the label widget on the master window. relx=0 is the left edge of the window and relx=1.0 is the right edge of the window so relx=0.5 is the center of the window. relx=0.5, y=15 means make a point in the center of the window down 15 pixels from the top of the window. The left edge of the label widget is placed at that point so anchor='center' means you center the label widget over the point.

Upvotes: 2

Related Questions