问题
- 如何更改
ScrolledText
Tkinter 小部件中特定行的对齐方式? - 我最初的错误的原因是什么?
背景
我目前正在开发 Tkinter 文本框应用程序,并且正在寻找更改行对齐的方法。最终,我希望能够将特定行从LEFT ALIGN更改为RIGHT ALIGN等等。现在,我只需要了解如何更改整个文本框的对齐方式,尽管我们将不胜感激进一步的帮助。我一直在使用 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 的建议更改了代码。我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"