tkinter孪生文本框(peer)
- Text的peers
- 需要自己命名
- 创建孪生文本框
- 可能的用途
Text的peers
这个标题的名字一看有点抽象,其实是这样的。
在tkinter中,文本框(Text)有两个派生自peer
的方法,peer_create
, peer_names
,也没有什么人介绍过。tcl官网的解释如下:
This command is used to create and query widget peers.
两个方法的解释:
pathName peer create newPathName ?options?
Creates a peer text widget with the given newPathName, and any optional standard configuration options (as for the text command). By default the peer will have the same start and end line as the parent widget, but these can be overridden with the standard configuration options.
pathName peer names
Returns a list of peers of this widget (this does not include the widget itself). The order within this list is undefined.
在python中,同样“本地化”了这两种方法,就是开头提到的那两个,不过,和tkinter风格有很大出入,那是就peer_create
这个方法。
需要自己命名
没错,tkinter在tcl层面的控件命名自行完成,不需要开发者去在意tcl中控件的层级问题。但是,peer_create
这个方法,偏偏就有一个newPathName
参数,需要输入一个新的、tcl可理解的控件层级命名。不过,本篇文章中,只是浅浅看看这个功能到底是什么样的,并且讨论一点可能会用到的地方。因此,我们就用peert='.!peert'
来命名这个孪生文本框。
注意,
.!peer
为顶级窗口子控件,实际使用时,要注意父级窗口或控件。比如,孪生文本框被创建在子窗口中,就要用
peert='.!Toplevel2.!peert'
。在一个Frame中,就要用
peert='.!Toplevel1.!Frame1.!peert'
。
创建孪生文本框
假如我们已经创建好了一个文本框,text
。
peert='.!peert'
text.peer_create(peert,borderwidth=0,relief='flat',insertbackground='#000000', insertborderwidth=1, wrap='char')
text.tk.call('pack', peer)
好了,就这样,完事。
看看效果:
没错,就是这么简单。
可能的用途
这个,其实我也没有想到很多。
-
同样的内容,在不同的窗口或窗口部位展示。
为了方便同步,干脆直接使用peer,而且用户操作任何一个编辑框都会同步更改。
-
文本缩略图。
孪生之所以是孪生,tag样式都可以同步。
☀tkinter创新☀