如何在 Python/Tkinter 中将 StringVar 连接到文本小部件? [英] How can I connect a StringVar to a Text widget in Python/Tkinter?

查看:45
本文介绍了如何在 Python/Tkinter 中将 StringVar 连接到文本小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我希望 Text 小部件的主体在 StringVar 发生变化时发生变化.

Basically, I want the body of a Text widget to change when a StringVar does.

推荐答案

简而言之,你不能.至少,不是没有做额外的工作.文本小部件不直接支持 variable 选项.

Short version is, you can't. At least, not without doing extra work. The text widget doesn't directly support a variable option.

如果您想自己完成所有工作,可以在变量上设置跟踪,以便使文本小部件保持最新,并且您可以向文本小部件添加绑定以保持变量最新,但是 Tkinter 没有直接内置任何东西来自动执行此操作.

If you want to do all the work yourself it's possible to set up a trace on a variable so that it keeps the text widget up to date, and you can add bindings to the text widget to keep the variable up to date, but there's nothing built directly into Tkinter to do that automatically.

这不被直接支持的主要原因是文本小部件可以有更多的 ascii 文本——它可以有不同的字体和颜色、嵌入的小部件和图像以及标签.

The main reason this isn't directly supported is that the text widget can have more than just ascii text -- it can have different fonts and colors, embedded widgets and images, and tags.

这篇关于如何在 Python/Tkinter 中将 StringVar 连接到文本小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆