如何在Tkinter Text小部件中居中文本?

时间:2017-03-02 16:20:45

标签: python tkinter

我正在学习python而且我正在完成迷你项目。我做了一个8球计划,差不多完成了。令我讨厌的一件事是输出到用户的文本不是居中的。

我该怎么做?我尝试过以下但仍然没有运气。

T1.tag_configure("center", justify='center')
T1.tag_add("center", 1.0, "end")

我的代码是here。谢谢你的帮助!

3 个答案:

答案 0 :(得分:2)

Text窗口小部件最初为空,因此T1.tag_add("center", "1.0", "end")无效,但如果您在添加标记之前在窗口小部件中插入文本,则用户随后插入的文本将保持居中:

import Tkinter as tk

root = tk.Tk()

T1 = tk.Text(root)
T1.tag_configure("center", justify='center')
T1.insert("1.0", "text")
T1.tag_add("center", "1.0", "end")
T1.pack()

root.mainloop()

由于在完整代码中使用了一行Text小部件,因此您可以使用Entry代替:

tk.Entry(root, justify='center')

答案 1 :(得分:1)

这是您的代码,其中显示了修改,因此它可以满足您的需求。这是通过添加一个名为insert_centered()的新函数并在任何地方调用文本小部件的内容来实现的。

请注意,新函数是通过文本小部件传递的,而不是将全局变量的名称硬编码到其中。

import random
import PIL.ImageTk
import PIL.Image
from Tkinter import *


def thinking():
#    T1.insert(INSERT, 'Thinking...')  # REMOVED
    insert_centered(T1, 'Thinking...')  # ADDED
    T1.after(3000, empty_textbox)


def empty_textbox():
    T1.delete("1.0", END)


def new_question(event=None):
    empty_textbox()
    if len(entry.get()) == 0:
#        T1.insert(END, 'Ask a question')  # REMOVED
        insert_centered(T1, 'Ask a question')  # ADDED
    else:
        thinking()
        T1.after(3000, give_answer)


def give_answer():
    answers = ['Signs point to yes.',
               'Yes.',
               'Reply hazy',
               'try again.',
               'Without a doubt.',
               'My sources say no.',
               'As I see it, yes.',
               'You may rely on it.',
               'Concentrate and ask again.',
               'Outlook not so good.',
               'It is decidedly so.',
               'Better not tell you now.',
               'Very doubtful.',
               'Yes - definitely.',
               'It is certain.',
               'Cannot predict now.',
               'Most likely.',
               'Ask again later.',
               'My reply is no.',
               'Outlook good.',
               'Don\'t count on it.']
    answer = random.randint(1, 20)
#    T1.insert(END, answers[answer])  # REMOVED
    insert_centered(T1, answers[answer])  # ADDED


def end():
    exit()


def clear():
    entry.delete(0, 'end')
    T1.delete('1.0', END)


def insert_centered(text_widget, text):        # ADDED
    text_widget.delete("1.0", END)             # ADDED
    text_widget.insert("1.0", text, "center")  # ADDED


root = Tk()

load = PIL.Image.open("8-ball.png")
render = PIL.ImageTk.PhotoImage(load)

img = Label(root, image=render)
img.image = render
img.pack()

topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)

entry = Entry(root, width=40)
entry.pack()

T1 = Text(root, width=26, height=1)
T1.tag_configure("center", justify='center')
T1.tag_add("center", 1.0, "end")
T1.pack()

root.bind('<Return>', new_question)

button1 = Button(bottomFrame, text="Ask", fg="blue", command=new_question)
button2 = Button(bottomFrame, text="Clear", fg="blue", command=clear)
button3 = Button(bottomFrame, text="Quit", fg="blue", command=end)

button1.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)

root.mainloop()

为方便其他读者,这里有一张可用于测试的图片:

test image

答案 2 :(得分:0)

有一个简单的修改或技巧,您可以执行此操作以获取所需的结果,只需添加一个空格,然后使用您的代码即可。代码在这里:-

import tkinter as tk
root = tk.Tk()
T1 = tk.Text(root)
T1.tag_configure("center", justify='center')
T1.insert(1.0, " ")
T1.tag_add("center", "1.0", "end")
T1.pack()
root.mainloop()