问题
- 如何在Tkinter的
ScrolledText
小部件中更改特定行的对齐方式? - 我的原始错误是什么原因造成的?
背景
我正在开发一个 Tkinter 文本框应用程序,并寻找改变行对齐方式的方法。最终,我想能够将特定行从左对齐更改为右对齐等等。现在,我只需要找出如何更改整个文本框的对齐方式,但后续的帮助将非常感激。我一直在使用 Effbot.org 来帮助我完成这个任务。下面是我的ScrolledText
小部件的代码:
代码
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()
当我运行这个程序时,会出现错误。
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
>>>
如果我将
justify
参数在font
参数之后修改,我会得到一个不同的错误,以消息框的形式出现。There's an error in your program:
*** non-keyword arg after keyword arg (MyTextv5.py, line 61)
编辑
根据abarnert的建议,我修改了代码。在self.maintextFrame
后面添加了justify = CENTER,
。尽管如此,我仍然遇到了另一个错误。
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"