tkinter.Text:将变量绑定到窗口小部件文本内容

bob*_*yer 4 python tkinter python-3.x

使用Python 3.3平台独立于此问题.

对于Entry窗口小部件,您可以将变量绑定到此窗口小部件的文本内容,如下所示(请注意构造函数中的textvariable参数Entry):

var = tkinter.StringVar()
entryField = tkinter.Entry(master, textvariable=var)
e.pack()

var.set("a new value") # entryField text now updated with this value
s = var.get() # whatever text now appears in entryField
Run Code Online (Sandbox Code Playgroud)

Text然而,对于小部件,没有这样的变量绑定功能.Text如果感兴趣的话,类定义应该可以从%python dir%/ Lib/tkinter/__ init__.py中的第2927行开始,适用于Windows版本中的Python 3.3.

如何使用Text窗口小部件最好地模拟此变量绑定功能?我的想法是将一个小部件绑定tkinter.StringVar到一个Text小部件,然后获取/设置所有文本.

更新:

我最终继承tkinter.Frame作为一个Text包装器,它接受一个textvariable期望作为tkinter.Variable实例的构造函数参数.我下面的例子中我唯一没有继承的Text原因只是因为我也想要一个滚动条,但这并不重要.

以下是我的实验代码.为了与我原来的问题以及问题如何解决(?)完全相关,重要的是self.textvariable.get = self.GetTextself.textvariable.set = self.SetText.基本上,我正在覆盖传入tkinter.Variable对象的get和set方法到我自己的设备......

class TextExtension( tkinter.Frame ):
    """Extends Frame.  Intended as a container for a Text field.  Better related data handling
    and has Y scrollbar now."""


    def __init__( self, master, textvariable = None, *args, **kwargs ):
        self.textvariable = textvariable
        if ( textvariable is not None ):
            if not ( isinstance( textvariable, tkinter.Variable ) ):
                raise TypeError( "tkinter.Variable type expected, {} given.".format( type( textvariable ) ) )
            self.textvariable.get = self.GetText
            self.textvariable.set = self.SetText

        # build
        self.YScrollbar = None
        self.Text = None

        super().__init__( master )

        self.YScrollbar = tkinter.Scrollbar( self, orient = tkinter.VERTICAL )

        self.Text = tkinter.Text( self, yscrollcommand = self.YScrollbar.set, *args, **kwargs )
        self.YScrollbar.config( command = self.Text.yview )
        self.YScrollbar.pack( side = tkinter.RIGHT, fill = tkinter.Y )

        self.Text.pack( side = tkinter.LEFT, fill = tkinter.BOTH, expand = 1 )


    def Clear( self ):
        self.Text.delete( 1.0, tkinter.END )


    def GetText( self ):
        text = self.Text.get( 1.0, tkinter.END )
        if ( text is not None ):
            text = text.strip()
        if ( text == "" ):
            text = None
        return text


    def SetText( self, value ):
        self.Clear()
        if ( value is not None ):
            self.Text.insert( tkinter.END, value.strip() )
Run Code Online (Sandbox Code Playgroud)

旁注:很明显,我是基于间距的另一种语言.对不起,我帮不上忙.

我想我回答了自己的问题.是否正确的做法是覆盖tkinter.Variable传递给我的函数的对象的已知方法,就像我刚才所做的那样是一个单独的问题,我将不得不问/研究,即使这是一个永远不会是私有的代码.在我的应用之外使用.我承认这确实是一个问题,这是否是一个有效的解决方案.

Bry*_*ley 6

如果你愿意危险地生活,可以挂钩文本小部件的内部,并在内容改变时让它调用一个函数,无论它如何改变.

诀窍是用代理替换底层的tk widget命令.此代理负责执行真正的文本小部件所做的任何操作,然后发送虚拟事件,如果它执行的操作是插入或删除文本.

有了这个,只需要设置一个绑定到该事件,并在变量上放置一个读取跟踪.当然,如果您尝试在文本中插入小部件或图像,它们将不会反映在textvariable中.

这是一个快速而肮脏的例子,没有任何真实的测试.这使用了我用来在文本小部件中实现行号的相同技术(请参阅/sf/answers/1146266341/)

import Tkinter as tk
import random
import timeit

class TextWithVar(tk.Text):
    '''A text widget that accepts a 'textvariable' option'''
    def __init__(self, parent, *args, **kwargs):
        try:
            self._textvariable = kwargs.pop("textvariable")
        except KeyError:
            self._textvariable = None

        tk.Text.__init__(self, parent, *args, **kwargs)

        # if the variable has data in it, use it to initialize
        # the widget
        if self._textvariable is not None:
            self.insert("1.0", self._textvariable.get())

        # this defines an internal proxy which generates a
        # virtual event whenever text is inserted or deleted
        self.tk.eval('''
            proc widget_proxy {widget widget_command args} {

                # call the real tk widget command with the real args
                set result [uplevel [linsert $args 0 $widget_command]]

                # if the contents changed, generate an event we can bind to
                if {([lindex $args 0] in {insert replace delete})} {
                    event generate $widget <<Change>> -when tail
                }
                # return the result from the real widget command
                return $result
            }
            ''')

        # this replaces the underlying widget with the proxy
        self.tk.eval('''
            rename {widget} _{widget}
            interp alias {{}} ::{widget} {{}} widget_proxy {widget} _{widget}
        '''.format(widget=str(self)))

        # set up a binding to update the variable whenever
        # the widget changes
        self.bind("<<Change>>", self._on_widget_change)

        # set up a trace to update the text widget when the
        # variable changes
        if self._textvariable is not None:
            self._textvariable.trace("wu", self._on_var_change)

    def _on_var_change(self, *args):
        '''Change the text widget when the associated textvariable changes'''

        # only change the widget if something actually
        # changed, otherwise we'll get into an endless
        # loop
        text_current = self.get("1.0", "end-1c")
        var_current = self._textvariable.get()
        if text_current != var_current:
            self.delete("1.0", "end")
            self.insert("1.0", var_current)

    def _on_widget_change(self, event=None):
        '''Change the variable when the widget changes'''
        if self._textvariable is not None:
            self._textvariable.set(self.get("1.0", "end-1c"))


class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.textvar = tk.StringVar()
        self.textvar.set("Hello, world!")

        # create an entry widget and a text widget that
        # share a textvariable; typing in one should update
        # the other
        self.entry = tk.Entry(self, textvariable=self.textvar)
        self.text = TextWithVar(self,textvariable=self.textvar, 
                                borderwidth=1, relief="sunken", 
                                background="bisque")

        self.entry.pack(side="top", fill="x", expand=True)
        self.text.pack(side="top",fill="both", expand=True)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()
Run Code Online (Sandbox Code Playgroud)

  • TCL.这是为tkinter提供动力的技术.Tkinter只是嵌入式tcl解释器的一个瘦接口. (2认同)