gpt4 book ai didi

python - 使用 tkinter 操作文本

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:19 24 4
gpt4 key购买 nike

我的计划是显示字符串,然后根据字母的索引突出显示特定字母。为了给你一个想法,它最终应该是这样的: click me

我想使用 tkinter 和它的 Canvas ,但现在我知道,它无法突出显示字符串中的特定字母。

你知道我该怎么做吗?我也愿意使用 tkinter 以外的其他东西。

谢谢

最佳答案

所以,如果我理解正确的话,您只想添加一些文本,然后突出显示它吗?我建议你去教程点,他们非常清楚如何做到这一点,链接如下:http://www.tutorialspoint.com/python/tk_text.htm

这里还有一些代码:

from Tkinter import *

def onclick():
pass

root = Tk()
text = Text(root)
text.insert(INSERT, "Hello.....")
text.insert(END, "Bye Bye.....")
text.pack()

text.tag_add("here", "1.0", "1.4")
text.tag_add("start", "1.8", "1.13")
text.tag_config("here", background="yellow", foreground="blue")
text.tag_config("start", background="black", foreground="green")
root.mainloop()

因此,要移动文本,我看到的唯一解决方案是将文本设为变量,然后添加如下内容:

text.config(text = '    '+'\n'+'\n'+our_variable_we_used_for_our_text)

这会插入一些空格和一些换行符,并不完美,但我不认为还有其他东西,

附注

\n 代表换行符

关于python - 使用 tkinter 操作文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38041715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com