如何使用 tkinter tag_config?蟒蛇 3.7.3 [英] How to use tkinter tag_config? Python 3.7.3

查看:55
本文介绍了如何使用 tkinter tag_config?蟒蛇 3.7.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想做一个文本编辑器,但我在这部分卡住了,我希望它改变实时输入的某个单词的颜色,例如:假设我输入 print 我希望它自动将前景色从默认的黑色更改为蓝色.我真的不知道您是否必须使用 tag_configure 来执行此操作,但如果可以,请有人帮我解决,谢谢.

So I wanted to make a text editor and I've gotten stuck on this part, I want it to change the color of a certain word thats typed in in real time, for example: Lets say I type print I want it to automatically change the foreground color from the default black color to lets say blue. I Don't really know if you have to use tag_configure to do this but if so can someone please help me work it, thanks.

代码:

from tkinter import *

root = Tk()

text = Text(root)
text.grid(row=0)

def Event(event):
    text.tag_configure("print", foreground="blue")

#This is a KeyBind to trigger the Function: Event
root.bind("<Return>", Event)

root.mainloop()

一个星期以来我一直在努力解决这个问题,我真的希望有人能帮我解决这个问题,如果有人可以至少向我提供一些信息或示例那将不胜感激.

I've been trying to figure this out for a week now, and I really hope someone can help me with this, if someone can provide me at least some information or an example that would be really appreciated.

推荐答案

如果您想在键入时更改文本的颜色,您需要做一些事情:

If you want to change the color of text as you're typing it in, you need to do a few things:

  1. 定义具有所需颜色的标签.你已经得到了:

  1. define a tag with the desired coloration. You're already got:

text.tag_configure("print", foreground="blue")

哪个有效.您可能希望能够触发其他事件来更改颜色以应用于新键入的文本,否则您的编辑器将非常无聊.您可以为每个按钮设置几个不同颜色的按钮,等等.您可能需要多个标签,因为如果您更改打印"的配置标签,已经用该标签名称标记的所有内容都将更改以匹配新配置.

which works. You probably want to be able to trigger other events which change the color to apply for the newly typed text, or your editor will be quite boring. You can have a few buttons with a different color for each, etc. You'll probably want multiple tags, because if you change the config of the "print" tag, everything that is already tagged with that tag name will change to match the new config.

  1. 然后您必须在输入文本时将标记应用于文本.可能有几种方法可以做到这一点,但是您可以通过为 " 设置绑定来处理输入的每个字符;<Key>>> 事件.每次触发事件时,因为输入了一个新字符,返回并应用标签打印".到那个角色.
  1. then you have to apply the tag to the text as you're typing it in. There are probably a few ways to do this, but you can process each character as it's typed by setting up a binding for "<Key>" events. Each time the event is triggered, because a new char has been typed, go back and apply the tag "print" to that character.

所以,大致如下:

root.bind("<Key>", on_key)

连同:

def on_key(event=None):
    text.tag_add("print", "INSERT - 1c", "INSERT")

这应该大致给了你你想要的东西,但是当它设置好时,它仍然不会太有趣.您可以有多个标签,每个标签都配置为在视觉上不同(不同的前景色和背景色等),并根据需要将不同的标签应用于您输入的内容.

That should roughly give you what you want, but as it's set up, it still won't be too interesting. You can have multiple tags, each configured to look visually different (different foreground and background colors, etc), and apply different tags to what you're typing in, as you see fit.

INSERT 是一个特殊的文本小部件标记,表示插入点/光标在小部件中的位置.当您键入时,INSERT 会一直向右移动,总是在您键入的内容之后.所以,插入 - 1c"是上一个位置,指向刚刚输入的字符.

INSERT is a special Text widget mark which represents where the insertion point/cursor is in the widget. As you type, INSERT keeps moving to the right, always just after what you've typed. So, "INSERT - 1c" is the previous location and points to the character that was just typed.

快乐编码!

这篇关于如何使用 tkinter tag_config?蟒蛇 3.7.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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