我正在 Mac OS X 10.11.5 上使用 tcl/tk 8.5.9 和 python 3.5.1 开发 gui。
我正在尝试创建一种 tk.text 小部件列表,这些小部件具有固定宽度并在更改时调整高度以适应其内容(由用户或以编程方式)。这应该考虑到真实的和包裹的显示线。
我使用 Tkinter Resize text to contents 的答案让这个工作正常进行和 here 。但是,调整用户输入仅适用于最后创建的文本小部件。添加新文本小部件后,所有先前创建的文本小部件都不再调整大小。
我认为问题可能出在处理绑定(bind)标签的行中。作为 tkinter、python 的新手,而且对于一般编程也比较陌生,我不确定我是否正确理解了绑定(bind)标签。
非常感谢任何帮助,这是我的代码:
from tkinter import *
class ResizingText(Text):
def __init__(self, parent, *args, **kwargs):
Text.__init__(self, master=parent, wrap='word', *args, **kwargs)
# event binding on resize because text.count method for displaylines returns a wrong number when widget is instantiated
self.bind('<Configure>', self.update_size)
bindtags = list(self.bindtags())
bindtags.insert(2, "custom")
self.bindtags(tuple(bindtags))
self.bind_class("custom", "<Key>", self.update_size)
def update_size(self, event):
if self.winfo_width() > 1:
self.unbind('<Configure>')
displaylines = self.count("1.0", "end", "displaylines")
self.config(height=displaylines)
root = Tk()
dynamic_text_1 = ResizingText(root, width=60)
dynamic_text_1.insert('1.0', "Longer text that is long enough to be wrapped into multiple display lines")
dynamic_text_1.grid(column=0, row=0)
# this text widget does not behave as expected:
# no resizing after user inputs a line break or a line long enough to be wrapped
dynamic_text_2 = ResizingText(root, width=60)
dynamic_text_2.insert('1.0', "Longer text that is long enough to be wrapped into multiple display lines")
dynamic_text_2.grid(column=0, row=1)
# this text widget behaves as expected
root.mainloop()
我通过配置绑定(bind)标签并在每次文本小部件获得焦点时重置按键事件上的事件绑定(bind)(FocusIn 上的事件绑定(bind))来解决此问题。
可能不是一个干净的解决方案,但它似乎工作正常:
from tkinter import *
class ResizingText(Text):
def __init__(self, parent, *args, **kwargs):
Text.__init__(self, master=parent, wrap='word', *args, **kwargs)
# event binding on resize because text.count method for displaylines returns a wrong number when widget is instantiated
self.bind('<Configure>', self.update_size)
self.configure_bindtags(event=None)
# solution: additional binding that resets the binding for <Key> events whenever the text widget gets the focus
self.bind('<FocusIn>', self.configure_bindtags)
def configure_bindtags(self, event):
bindtags = list(self.bindtags())
bindtags.insert(2, "custom")
self.bindtags(tuple(bindtags))
self.bind_class("custom", "<Key>", self.update_size)
def update_size(self, event):
if self.winfo_width() > 1:
self.unbind('<Configure>')
displaylines = self.count("1.0", "end", "displaylines")
self.config(height=displaylines)
root = Tk()
dynamic_text_1 = ResizingText(root, width=60)
dynamic_text_1.insert('1.0', "Longer text that is long enough to be wrapped into multiple display lines")
dynamic_text_1.grid(column=0, row=0)
dynamic_text_2 = ResizingText(root, width=60)
dynamic_text_2.insert('1.0', "Longer text that is long enough to be wrapped into multiple display lines")
dynamic_text_2.grid(column=0, row=1)
root.mainloop()
我是一名优秀的程序员,十分优秀!