I've run into an interesting issue regarding the Text
widget in Tkinter I can't seem to understand. Google has provided no answers either. It seems like Tkinter has a single line character limit on the Text()
widget of 4096 character when text wrapping is disabled. Is there a way to change this limit or force a text wrap after 4095 characters? I've seen the wraplength
parameter on other widgets but nothing for the Text
widget.
Example code:
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()
What's really strange is if you click or highlight where the "b"s should be printed they show up all of sudden? Why did they disappear in the first place?
Python version: 2.7.5
OS: Windows 7
UPDATE:
This seems to be a platform issue with Windows 7. Still not sure why it occurs or if it can be remedied easily.
Screenshots:
This is what the application looks like when first started. 'b's are missing:
Once I give the 'b's focus they show up all of sudden:
Once I remove focus from the 'b's they disappear.
i have found a partial solution on another site: here
it works by subclassing the text widget to watch the line length and insert an extra newline character when the limit is reached
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()
this method has several obvious limitations, firstly it is actually adding characters to the data that is being entered (which may not be desirable) and secondly it only wraps by character, so it could wrap in the middle of a word, but there are ways in which this could be implemented aswell.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.