Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用Tkinter和滚动文本在python中设置文本对齐方式_Python_Tkinter - Fatal编程技术网

尝试使用Tkinter和滚动文本在python中设置文本对齐方式

尝试使用Tkinter和滚动文本在python中设置文本对齐方式,python,tkinter,Python,Tkinter,我正在尝试将文本设置为放置在中心或滚动文本小部件中。这是怎么做到的? 我尝试过设置justify=center,但这会给出“未知选项”-justify 我还能做什么呢?这是我的代码,如果有帮助的话 #Gui import tkinter as tk import tkinter.scrolledtext as tkst from tkinter import * def center_window(width=300, height=200): # get screen width an

我正在尝试将文本设置为放置在中心或滚动文本小部件中。这是怎么做到的? 我尝试过设置justify=center,但这会给出“未知选项”-justify 我还能做什么呢?这是我的代码,如果有帮助的话

#Gui
import tkinter as tk
import tkinter.scrolledtext as tkst
from tkinter import *
def center_window(width=300, height=200):
    # get screen width and height
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    # calculate position x and y coordinates
    x = (screen_width/2) - (width/2)
    y = (screen_height/2) - (height/2)
    root.geometry('%dx%d+%d+%d' % (width, height, x, y))



root=Tk()
root.title("Writing is hard!") 
center_window(1200,600)

heading = Label(root, text="Welcome to the app which checks your content for you!", font=("arial",30,"bold"), fg="steelblue")
label1=Label(root, text="Please type in your article: ", font=("arial",20,"bold"), fg="lightgreen")
ArticleTextBox = tkst.ScrolledText(width=100, height=20, justify='CENTER')
def do_it():
    article=ArticleTextBox.get(0.0,tk.END)
    print("Hello "+name.get())
    print(article)
work= Button(root, text="work", width=30,height=5,bg="lightblue",command=do_it)

#Puts everything on screen
heading.pack()
label1.pack()
ArticleTextBox.pack()
work.pack()
root.mainloop()

justify
是文本标记的属性。您可以将标记应用于所有文本,然后在该标记上设置
justify
属性

ArticleTextBox.insert("end", "hello\nworld!", ("centered",))
ArticleTextBox.tag_configure("centered", justify="center")


顺便说一下,在代码中使用的索引
0.0
是无效的索引。Tkinter会接受它,但从技术上讲它是无效的。文本索引是字符串,不是浮点数,第一行从1(一)开始,而不是零。因此,
0.0
应该是
“1.0”“
是文本标记的属性。您可以对所有文本应用标记,然后在该标记上设置
justify
属性

ArticleTextBox.insert("end", "hello\nworld!", ("centered",))
ArticleTextBox.tag_configure("centered", justify="center")


顺便说一下,在代码中使用的索引
0.0
是无效的索引。特金特会接受的,但从技术上来说是无效的。文本索引是字符串,而不是浮点数,第一行从1(1)开始,而不是零。因此,
0.0
应该是
“1.0”

我看不到您的代码中有任何地方在文本框中添加任何内容。谢谢Bryan,但应该是这样的。这是因为该程序的用户将输入Scrolled文本,有点像文字处理器。我看不到您的代码中有任何地方向文本框添加任何内容。谢谢Bryan,但应该是这样。这是因为程序的用户将在ScrolledText中键入内容,有点像文字处理器。