欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > tkinter孪生文本框

tkinter孪生文本框

2024/10/24 14:18:37 来源:https://blog.csdn.net/tinga_kilin/article/details/140851978  浏览:    关键词:tkinter孪生文本框

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)

好了,就这样,完事。

看看效果:

在这里插入图片描述

没错,就是这么简单。

可能的用途

这个,其实我也没有想到很多。

  1. 同样的内容,在不同的窗口或窗口部位展示。

    为了方便同步,干脆直接使用peer,而且用户操作任何一个编辑框都会同步更改。

  2. 文本缩略图。

    孪生之所以是孪生,tag样式都可以同步。
    在这里插入图片描述

☀tkinter创新☀

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com