如何在 Tkinter 文本框上设置对齐方式 [英] How to set justification on Tkinter Text box

查看:309
本文介绍了如何在 Tkinter 文本框上设置对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

  1. 如何在 Tkinter 的 ScrolledText 小部件中更改特定行的对齐方式?
  2. 我最初出现错误的原因是什么?
  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?

背景

我目前正在开发 Tkinter 文本框应用程序,并且正在寻找更改行对齐方式的方法.最终,我希望能够将特定行从 LEFT ALIGN 更改为 RIGHT ALIGN 等等.现在,我只需要找出如何更改整个文本框,但进一步的帮助将不胜感激.我一直在使用 Effbot.org 来帮助我完成任务.这是我的 ScrolledText 小部件代码:

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:

代码

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
>>> 

如果我将 justify 参数更改为 font 参数之后,我会以消息框的形式收到不同的错误.

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)

编辑

我根据 abarnert 的建议更改了代码.我在 self.maintextFrame 之后添加了 justify = CENTER,.尽管如此,我还是遇到了另一个错误.

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 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"

推荐答案

您正在使用名为 justify 的选项,但文本小部件不存在这样的选项.您是否在某处阅读一些文档,其中说 justify 是一个有效选项?如果是这样,您需要停止阅读该文档.

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.

然而,文本标签有一个 justify 选项.您可以将标签配置为有理由,然后将该标签应用于一行或多行.我从来没有使用过 ScrolledText 小部件,所以我不知道它是否具有与纯文本小部件相同的方法,但是使用纯文本小部件你会做这样的事情:

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")

在上面的例子中,我们正在创建和配置一个名为center"的标签.标签可以有您想要的任何名称.对于center"标签,我们给它一个center"的理由,然后将该标签应用到小部件中的所有文本.

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.

您可以通过调整 tag_add 方法的参数来标记单个行.您还可以在使用 insert 方法插入文本时提供标签列表.

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.

这篇关于如何在 Tkinter 文本框上设置对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆