简体   繁体   English

如何在 tkinter 标签中居中文本,如 Microsoft Word 居中打字模式

[英]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.我正在尝试在 tkinter 上创建一个页面到页面的系统,但是从右侧开始的文本看起来有点奇怪,如果文本从标签的中心均匀分布,我会更喜欢它。
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.点在那里是因为 Stackoverflow 不允许超过一个空格,所以忽略那些。 It should look just like Microsoft Word's centered writing它应该看起来像 Microsoft Word 的居中写作
Here is the basic code I have, I tried using anchor = CENTER but that did not work as intended.这是我拥有的基本代码,我尝试使用anchor = CENTER但没有按预期工作。 Sorry if this is basic I'm new to Tkinter对不起,如果这是基本的我是 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)

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.默认情况下,tk 标签的文本在标签内居中,因此这里的问题是标签本身没有居中。

The origin point for place is the upper most left corner x=0, y=0. place 的原点是最左上角 x=0, y=0。 Using textLabel.place(relx=0.5, y=15, anchor='center') centers the label widget on the master window.使用textLabel.place(relx=0.5, y=15, anchor='center')将标签小部件置于主窗口的中心。 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是窗口的左边缘, relx=1.0是窗口的右边缘,所以relx=0.5是窗口的中心。 relx=0.5, y=15 means make a point in the center of the window down 15 pixels from the top of the window. relx=0.5, y=15表示在距离窗口顶部向下 15 个像素的窗口中心创建一个点。 The left edge of the label widget is placed at that point so anchor='center' means you center the label widget over the point.标签小部件的左边缘位于该点,因此anchor='center'意味着您将标签小部件anchor='center'放置在该点上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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