Python 如何在 Tkinter 文本框上设置对齐方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15014980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set justification on Tkinter Text box
提问by xxmbabanexx
Question
题
- How can I change the justification of specific lines in the
ScrolledTextwidget in Tkinter? - What was the reason for my original errors?
- 如何更改
ScrolledTextTkinter 小部件中特定行的对齐方式? - 我原来的错误的原因是什么?
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 ALIGNto RIGHT ALIGNto 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 ScrolledTextwidget:
我目前正在开发 Tkinter 文本框应用程序,并且正在寻找更改行对齐方式的方法。最终,我希望能够将特定行从LEFT ALIGN更改为RIGHT ALIGN等等。现在,我只需要找出如何更改整个文本框的对齐方式,尽管将不胜感激进一步的帮助。我一直在使用 Effbot.org 来帮助我完成任务。这是我的ScrolledText小部件代码:
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 Programs\Text Editor\MyTextv5.py", line 132, in <module>
app = Application(root)
File "D:\Python Programs\Text Editor\MyTextv5.py", line 16, in __init__
self.mainTextFUN()
File "D:\Python Programs\Text Editor\MyTextv5.py", line 57, in mainTextFUN
justify(CENTER),
NameError: global name 'justify' is not defined
>>>
If I change the justifyargument to after the fontargument, I get a different error in the form of a messagebox.
如果我将justify参数更改为在参数之后font,我会以消息框的形式收到不同的错误。
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.
我根据 abarnert 的建议更改了代码。我justify = CENTER,在self.maintextFrame. 尽管如此,我还是遇到了另一个错误。
Traceback (most recent call last):
File "D:\Python Programs\Text Editor\MyTextv5.py", line 132, in <module>
app = Application(root)
File "D:\Python Programs\Text Editor\MyTextv5.py", line 16, in __init__
self.mainTextFUN()
File "D:\Python Programs\Text Editor\MyTextv5.py", line 60, in mainTextFUN
font = ("Times New Roman",12),
File "C:\Python27\lib\lib-tk\ScrolledText.py", line 26, in __init__
Text.__init__(self, self.frame, **kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2827, in __init__
Widget.__init__(self, master, 'text', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1974, in __init__
(widgetName, self._w) + extra + self._options(cnf))
TclError: unknown option "-justify"
回答by abarnert
The problem is your Python syntax, not anything related to tkinter:
问题是您的 Python 语法,与tkinter以下内容无关:
self.write = ScrolledText(self.maintextFrame,
justify(CENTER),
width = 100, height = 35, relief = SUNKEN,
That's not how you pass a keyword argument justifywith value CENTER; you're passing a positional argument, with value justify(CENTER). So, Python tries to calculate justify(CENTER)by looking for a function named justify, doesn't find one, and raises a NameError.
这不是您传递justify带有 value的关键字参数的方式CENTER;您正在传递一个带有 value 的位置参数justify(CENTER)。因此,Python 尝试justify(CENTER)通过查找名为 的函数来进行计算justify,但没有找到,并引发了一个NameError.
Moving it doesn't help, it just means you get a SyntaxErrorbefore you can even get to the NameError, because you've got a positional argument justify(CENTER)after the keyword argument font, and that's not allowed.
移动它没有帮助,它只是意味着您在到达SyntaxError之前就得到了NameError,因为justify(CENTER)在关键字参数之后有一个位置参数font,这是不允许的。
The right way to do it is the same way you're passing all the other keyword arguments in the very next line:
正确的方法与您在下一行中传递所有其他关键字参数的方式相同:
self.write = ScrolledText(self.maintextFrame,
justify = CENTER,
width = 100, height = 35, relief = SUNKEN,
By the way changing the justification to CENTERisn't going to change anything from left-aligned to right-aligned; it's going to change it from left-aligned to centered. Are you sure you didn't want RIGHT?
顺便说一下,将理由更改CENTER为不会改变从左对齐到右对齐的任何内容;它将把它从左对齐改为居中。你确定你不想要RIGHT?
Once you've fixed the syntax problems, you've got a new problem, which isrelated to tkinter. The ScrolledTextclass just passes through its keyword params to the Textclass, and the Textwidget doesn't have a justifyparameter.
一旦固定的语法问题,你已经有了一个新的问题,这是关系到Tkinter的。该ScrolledText班只是通过其关键字PARAMS到Text类和Text窗口小部件没有一个justify参数。
If you read the docs, justifyis used as a tag associated with a range of text, not with the widget. So, you have to use tag_configwith the justifyproperty to create a named tag config, then you can apply it to ranges of text inside the widget, as the examples show. So:
如果您阅读docs,justify则用作与一系列文本相关联的标签,而不是与小部件相关联。因此,您必须使用tag_config该justify属性来创建命名标签配置,然后您可以将其应用于小部件内的文本范围,如示例所示。所以:
self.write.tag_config('justified', justify=RIGHT)
self.write.insert(INSERT, 'Ancients of Mu-Mu', 'justified')
And of course this directly answers your other question:
当然,这直接回答了您的另一个问题:
How can I change the justification of specific lines in the ScrolledText widget in Tkinter?
如何更改 Tkinter 中 ScrolledText 小部件中特定行的对齐方式?
Either insert them with the right tag in the first place, or select them and apply the tag after the fact.
首先插入带有正确标签的它们,或者选择它们并在事后应用标签。
回答by mmgp
It is easy to understand why trying to set justify=CENTERfor a Textwidget fails: there is no such option for the widget.
很容易理解为什么尝试justify=CENTER为Text小部件设置失败:小部件没有这样的选项。
What you want is to create a tag with the parameter justify. Then you can insert some text using that tag (or you can insert any text and later apply the tag to a certain region):
您想要的是创建一个带有参数的标签justify。然后您可以使用该标签插入一些文本(或者您可以插入任何文本,然后将标签应用于某个区域):
import Tkinter
root = Tkinter.Tk()
text_widget = Tkinter.Text()
text_widget.pack(fill='both', expand=True)
text_widget.tag_configure('tag-center', justify='center')
text_widget.insert('end', 'text ' * 10, 'tag-center')
root.mainloop()
回答by Bryan Oakley
You're using an option named justify, but no such option exists for the text widget. Are you reading some documentation somewhere that says justifyis a valid option? If so, you need to stop reading that documentation.
您正在使用名为 的选项justify,但文本小部件不存在此类选项。您是否在某处阅读一些文档,说这justify是一个有效的选项?如果是这样,您需要停止阅读该文档。
There is, however, a justifyoption 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 ScrolledTextwidget 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:
但是,justify文本标签有一个选项。您可以将标签配置为有理由,然后将该标签应用于一行或多行。我从未使用过ScrolledText小部件,所以我不知道它是否具有与纯文本小部件相同的方法,但是使用纯文本小部件您可以执行以下操作:
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.
在上面的例子中,我们正在创建和配置一个名为“center”的标签。标签可以有您想要的任何名称。对于“center”标签,我们给它一个“center”的理由,然后将该标签应用于小部件中的所有文本。
You can tag individual lines by adjusting the arguments to the tag_addmethod. You can also give a list of tags when inserting text using the insertmethod.
您可以通过调整tag_add方法的参数来标记各个行。您还可以在使用insert方法插入文本时给出标签列表。
回答by Wiley
What maybe confusing people is that the documentation at http://effbot.org/tkinterbook/text.htm, referred to above, does in fact list justifyas an option for the Text widget and maybe there was at one time, (seems like an option a text widget should have) but the documentation at http://www.tcl.tk/man/tcl8.4/TkCmd/text.htmdoes not list a justifyoption, only the -justifytag. Note that a Label widget can handle multi-line text and does have a -justifyoption.
可能让人们感到困惑的是,上面提到的http://effbot.org/tkinterbook/text.htm上的文档实际上确实列出justify了 Text 小部件的一个选项,也许曾经有过(似乎是文本小部件应该具有的选项)但http://www.tcl.tk/man/tcl8.4/TkCmd/text.htm上的文档没有列出justify选项,只有-justify标签。请注意,标签小部件可以处理多行文本,并且确实有一个-justify选项。

