繁体   English   中英

tkinter Text()小部件上的4096单行字符限制?

[英]4096 single line character limit on tkinter Text() widget?

关于Tkinter中的Text小部件,我遇到了一个有趣的问题,我似乎无法理解。 谷歌也没有提供任何答案。 当禁用文本换行时,似乎Tkinter在4096字符的Text()小部件上有单行字符限制。 有没有办法更改此限制或强制4095字符后的文本换行? 我在其他小部件上看到了wraplength参数,但是没有看到Text小部件。

示例代码:

import Tkinter as tk

if __name__ == "__main__":
    root = tk.Tk()
    text = tk.Text(root)
    sb = tk.Scrollbar(root, orient="horizontal", command=text.xview)
    text.configure(xscrollcommand=sb.set)
    text.configure(wrap=tk.NONE)
    text.pack(fill="both", expand=True)
    sb.pack(side="bottom", fill="x")

    text.insert("end","a"*4095)
    text.insert("end","\n")
    text.insert("end","b"*4096)
    text.insert("end","\n")
    text.insert("end","c"*4095)

    root.mainloop()

真正奇怪的是,如果你点击或突出显示应该打印“b”的地方,它们会突然出现? 他们为什么一开始就消失了?

Python版本:2.7.5

操作系统:Windows 7

更新:

这似乎是Windows 7的平台问题。仍然不确定它为什么会发生或者它是否可以轻松解决。

截图:

这是首次启动时应用程序的样子。 'b'缺失:

在此输入图像描述

一旦我给出'b'的焦点,他们突然出现:

在此输入图像描述

一旦我从'b'移开焦点,他们就会消失。

我在另一个网站上找到了部分解决方案: 这里

它通过子类化文本小部件来观察行长度并在达到限制时插入额外的换行符

import Tkinter as tk

class WrapText(tk.Text):
    def __init__(self, master, wraplength=100, **kw):
        tk.Text.__init__(self, master, **kw)
        self.bind("<Any-Key>", self.check)
        self.wraplength = wraplength-1 

    def check(self, event=None):
        line, column = self.index(tk.INSERT).split('.')
        if event and event.keysym in ["BackSpace","Return"]: pass
        elif int(column) > self.wraplength: 
            self.insert("%s.%s" % (line,column),"\n")

    def wrap_insert(self, index, text):
        for char in text:
            self.check()
            self.insert(index, char)

if __name__ == "__main__":
    root = tk.Tk()
    text = WrapText(root, wraplength=4000)
    sb = tk.Scrollbar(root, orient="horizontal", command=text.xview)
    text.configure(xscrollcommand=sb.set)
    text.configure(wrap=tk.NONE)
    text.pack(fill="both", expand=True)
    sb.pack(side="bottom", fill="x")

##    text.tag_config("mystyle", background="yellow", foreground="red", wrap="char")

    text.wrap_insert("end","a"*4095)#,"mystyle")
    text.wrap_insert("end","\n")
    text.wrap_insert("end","b"*4095)
    text.wrap_insert("end","\n")
    text.wrap_insert("end","c"*4095)

    root.mainloop()

这个方法有几个明显的局限性,首先它实际上是为输入的数据添加字符(这可能是不可取的),其次它只是按字符包装,所以它可以包装在一个单词的中间,但有一些方法其中也可以实施。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM