Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
947 views
in Technique[技术] by (71.8m points)

python - How to set justification on Tkinter Text box

Question

  1. How can I change the justification of specific lines in the ScrolledText widget in Tkinter?
  2. What was the reason for my original errors?

Background

I am currently working on a Tkinter Text box application, and am looking for ways to change the justification of a line. Ultimately, I want to be able to change specific lines from LEFT ALIGN to RIGHT ALIGN to etc. For now, I simply need to find out how to change the justification of the entire text box, though further help will be greatly appreciated. I have been using Effbot.org to help me in my quest. Here is my code for the ScrolledText widget:

Code

def mainTextFUN(self):
    self.maintextFrame = Frame (root, width = 50, height = 1,
                           )

    self.maintextFrame.grid(row = 1, column = 1, sticky = N, columnspan = 1,
                            rowspan = 1)

    self.write = ScrolledText(self.maintextFrame,
                              justify(CENTER),
                              width = 100, height = 35, relief = SUNKEN,
                              undo = True, wrap = WORD,
                              font = ("Times New Roman",12),
                              )
    self.write.pack()

When I run this, I get an error.

Traceback (most recent call last):
  File "D:Python ProgramsText EditorMyTextv5.py", line 132, in <module>
    app = Application(root)
  File "D:Python ProgramsText EditorMyTextv5.py", line 16, in __init__
    self.mainTextFUN()
File "D:Python ProgramsText EditorMyTextv5.py", line 57, in mainTextFUN
justify(CENTER),
NameError: global name 'justify' is not defined
>>> 

If I change the justify argument to after the font argument, I get a different error in the form of a messagebox.

There's an error in your program:
*** non-keyword arg after keyword arg (MyTextv5.py, line 61)

EDIT

I changed the code based on a suggestion by abarnert. I added justify = CENTER, after self.maintextFrame. Despite this, I simply got another error.

Traceback (most recent call last):
  File "D:Python ProgramsText EditorMyTextv5.py", line 132, in <module>
    app = Application(root)
  File "D:Python ProgramsText EditorMyTextv5.py", line 16, in __init__
    self.mainTextFUN()
  File "D:Python ProgramsText EditorMyTextv5.py", line 60, in mainTextFUN
    font = ("Times New Roman",12),
  File "C:Python27liblib-tkScrolledText.py", line 26, in __init__
    Text.__init__(self, self.frame, **kw)
  File "C:Python27liblib-tkTkinter.py", line 2827, in __init__
    Widget.__init__(self, master, 'text', cnf, kw)
  File "C:Python27liblib-tkTkinter.py", line 1974, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
TclError: unknown option "-justify"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're using an option named justify, but no such option exists for the text widget. Are you reading some documentation somewhere that says justify is a valid option? If so, you need to stop reading that documentation.

There is, however, a justify option for text tags. You can configure a tag to have a justification, then apply that tag to one or more lines. I've never used a ScrolledText widget so I don't know if it has the same methods as a plain text widget, but with a plain text widget you would do something like this:

t = tk.Text(...)
t.tag_configure("center", justify='center')
t.tag_add("center", 1.0, "end")

In the above example, we are creating and configuring a tag named "center". Tags can have any name that you want. For the "center" tag we're giving it a justification of "center", and then applying that tag to all the text in the widget.

You can tag individual lines by adjusting the arguments to the tag_add method. You can also give a list of tags when inserting text using the insert method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...