简体   繁体   中英

How do I center text in a single line output Text widget in Tkinter?

I'm creating a translation app with python dictionary; I have and entry widget (for inputting keys) and a single line Text widget (for outputting the values. I want the text outputted in the Text widget to be aligned center. I have tried tag_configure method but it's still not centering; the outputted text is still aligned left. What do I do? Thanks. Below is the code:

That is, the text widget is initially empty until a button is clicked for text in the entry widget (ent) to be translated into the text widget (output). The problem is, the text that appears in the output widget is aligned left; I want to align it center.

#Entry field
ent=Entry(root,width=28,justify='center',font=('Times 18'),textvar=textin,bg='white')
ent.pack()

#Output field
output = tk.Text(root)
output=Text(root,width=28,height=1,font=('Times 18'),fg="black")
output.tag_configure("center",justify='center')
output.tag_add("center",1.0,"end")
output.pack()

You've added the tag "center" to all of the text in the widget, but there is no text in the widget. Tags have to be associated with at least one character for them to have an effect.

I found the solution: I merely replaced the Text widget with Entry and set the justification to center.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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