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

Python 4096 tkinter Text()小部件上的单行字符限制?,python,windows,user-interface,tkinter,Python,Windows,User Interface,Tkinter,关于Tkinter中的文本小部件,我遇到了一个有趣的问题,我似乎无法理解。谷歌也没有提供答案。当禁用文本包装时,Tkinter在Text()小部件上有一个4096个字符的单行字符限制。有没有办法更改此限制或在4095个字符后强制换行?我在其他小部件上看到了wraplength参数,但在Text小部件上没有看到 示例代码: import Tkinter as tk if __name__ == "__main__": root = tk.Tk() text = tk.Text(r

关于Tkinter中的
文本
小部件,我遇到了一个有趣的问题,我似乎无法理解。谷歌也没有提供答案。当禁用文本包装时,Tkinter在
Text()
小部件上有一个4096个字符的单行字符限制。有没有办法更改此限制或在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

操作系统:Windows7

更新:

这似乎是Windows7的平台问题。仍然不确定为什么会发生这种情况,或者是否可以轻松补救

截图:

这就是应用程序第一次启动时的样子。”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()
将Tkinter作为tk导入
类WrapText(tk.Text):
def初始功率(自功率、主功率、总功率=100,**kw):
tk.Text.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.bind(“,self.check)
self.wraplength=wraplength-1
def检查(自身,事件=无):
行,列=self.index(tk.INSERT).split('.'))
如果事件和event.keysym位于[“BackSpace”,“Return”]:通过
elif int(列)>self.wraplength:
self.insert(“%s.%s”%(行、列),“\n”)
def wrap_插入(自身、索引、文本):
对于文本中的字符:
self.check()
self.insert(索引,字符)
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
text=WrapText(根,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)
某人包装(侧边=“底部”,填充=“x”)
##text.tag_config(“mystyle”,background=“yellow”,front=“red”,wrap=“char”)
text.wrap_插入(“结束”,“a”*4095)#,“mystyle”)
text.wrap\u插入(“结束”,“结束”)
文本.换行插入(“结束”,“b”*4095)
text.wrap\u插入(“结束”,“结束”)
文本.换行插入(“结束”,“c”*4095)
root.mainloop()

此方法有几个明显的局限性:首先,实际上是将字符添加到正在输入的数据中(这可能不是理想的),其次它只是通过字符来封装,所以它可以在一个单词的中间包起来,但也有一些方法可以实现。

你在这个平台上运行什么?当我在OSX上运行代码时,我看到了额外的“b”。这似乎是windows平台的问题:你的代码在我的Fedora Linux系统上工作(另一个人说它在Mac上工作)。您可能希望这样标记它?无法在Vista(Python 2.7.5)和Windows 7 Enterprise(Python 2.7.6)上复制-在这两台机器上我都看到了额外的“b”。4096没有问题,但当我在行中放置5000个
b
s时,最后几百个
b
s覆盖了第一个(也就是说,它们既不是被截断也不是被包装,而是覆盖同一行的前几个
b
s,使它们看起来模糊而粗体)。04@Matt我看到了与您完全相同的东西,Windows7x64上的Python2.7.8,但是当我向右滚动至少两个字符时,b通常会出现