2017-11-07 1 views
0

텍스트를 가운데 맞춤 텍스트 또는 ScrolledText 위젯에 배치하려고합니다. 어떻게 이뤄지나요? 이Tkinter와 스크롤 된 텍스트를 사용하여 파이썬에서 텍스트 정렬을 설정하려고 시도했습니다.

#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() 
+0

텍스트 상자에 아무 것도 추가하지 않는 코드는 어디에도 없습니다. –

+0

Bryan에게 감사 드려요.하지만 그건 사실입니다. 이것은 프로그램의 사용자가 워드 프로세서와 같은 ScrolledText에 타이핑하기 때문입니다. –

답변

1

justify 텍스트 태그의 속성입니다 도움이된다면 내가 = 센터를 정당화 설정을 시도했지만 그 "-justify"알 수없는 옵션 "을 제공 내가 할 수있는 다른 무엇. 여기에 당신은 할 수 있습니다. 내 코드입니다 모든 텍스트에 태그를 적용하고 해당 태그에 justify 속성을 설정합니다. 그런데

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

를 코드에서 잘못된 인덱스 인덱스 0.0를 사용합니다. Tkinter를 그것을 받아 들일 것입니다, 그러나 기술 그것은 무효입니다. 텍스트 인덱스는 부동 소수점 수가 아닌 문자열이며 첫 번째 행은 0이 아닌 1에서 시작합니다. 따라서 0.0"1.0"이어야합니다.

관련 문제