Python 向tkinter文本小部件添加高级功能
我正在开发一个简单的消息传递系统,需要将以下内容添加到Tkinter文本小部件中:Python 向tkinter文本小部件添加高级功能,python,windows,text,tkinter,message,Python,Windows,Text,Tkinter,Message,我正在开发一个简单的消息传递系统,需要将以下内容添加到Tkinter文本小部件中: 拼写检查 更改字体的选项(在所选文本上) 更改字体颜色的选项(在选定文本上) 更改字体大小的选项(在所选文本上) 我知道tkinter文本小部件能够通过标记机制使用多种字体和颜色,但我不知道如何利用这些功能 如何使用文本小部件的功能实现这些功能?具体来说,我如何更改单词的字体系列、颜色和大小,以及如何使用这些来实现拼写检查之类的功能,拼写错误的单词会与文本的其他部分有不同的下划线或颜色。1)Tk没有集成的拼写检查
"""
Some text
hello
"""
from Tkinter import *
from Tkconstants import RIGHT, LEFT, Y, BOTH
from tkFont import Font
from ScrolledText import ScrolledText
def example():
import __main__
from Tkconstants import END
stext = ScrolledText(bg='white', height=10)
stext.insert(END, __main__.__doc__)
f = Font(family="times", size=30, weight="bold")
stext.tag_config("font", font=f)
stext.insert(END, "Hello", "font")
stext.pack(fill=BOTH, side=LEFT, expand=True)
stext.focus_set()
stext.mainloop()
if __name__ == "__main__":
example()
Tkinter文本小部件非常强大,但您必须自己完成一些高级功能。它没有内置的拼写检查或内置的加粗按钮等,但它们很容易实现。所有的功能都在小部件中,你只需要知道怎么做 下面的示例提供了一个按钮,用于切换高亮显示文本的粗体状态——选择一系列字符,然后单击该按钮添加粗体属性,然后删除粗体属性。对于字体和颜色来说,扩展这个示例应该很容易 拼写检查也很容易。下面的示例使用了/usr/share/dict/words中的单词(几乎可以肯定Windows 7上不存在这些单词,因此您需要提供一个合适的单词列表),这非常简单,因为当您按下空格键时,它只会检查拼写,但这只是为了将示例的代码大小保持在最小的水平。在现实世界中,当你进行拼写检查时,你会想更聪明一点
import Tkinter as tk
import tkFont
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
## Toolbar
self.toolbar = tk.Frame()
self.bold = tk.Button(name="toolbar", text="bold",
borderwidth=1, command=self.OnBold,)
self.bold.pack(in_=self.toolbar, side="left")
## Main part of the GUI
# I'll use a frame to contain the widget and
# scrollbar; it looks a little nicer that way...
text_frame = tk.Frame(borderwidth=1, relief="sunken")
self.text = tk.Text(wrap="word", background="white",
borderwidth=0, highlightthickness=0)
self.vsb = tk.Scrollbar(orient="vertical", borderwidth=1,
command=self.text.yview)
self.text.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(in_=text_frame,side="right", fill="y", expand=False)
self.text.pack(in_=text_frame, side="left", fill="both", expand=True)
self.toolbar.pack(side="top", fill="x")
text_frame.pack(side="bottom", fill="both", expand=True)
# clone the text widget font and use it as a basis for some
# tags
bold_font = tkFont.Font(self.text, self.text.cget("font"))
bold_font.configure(weight="bold")
self.text.tag_configure("bold", font=bold_font)
self.text.tag_configure("misspelled", foreground="red", underline=True)
# set up a binding to do simple spell check. This merely
# checks the previous word when you type a space. For production
# use you'll need to be a bit more intelligent about when
# to do it.
self.text.bind("<space>", self.Spellcheck)
# initialize the spell checking dictionary. YMMV.
self._words=open("/usr/share/dict/words").read().split("\n")
def Spellcheck(self, event):
'''Spellcheck the word preceeding the insertion point'''
index = self.text.search(r'\s', "insert", backwards=True, regexp=True)
if index == "":
index ="1.0"
else:
index = self.text.index("%s+1c" % index)
word = self.text.get(index, "insert")
if word in self._words:
self.text.tag_remove("misspelled", index, "%s+%dc" % (index, len(word)))
else:
self.text.tag_add("misspelled", index, "%s+%dc" % (index, len(word)))
def OnBold(self):
'''Toggle the bold state of the selected text'''
# toggle the bold state based on the first character
# in the selected range. If bold, unbold it. If not
# bold, bold it.
current_tags = self.text.tag_names("sel.first")
if "bold" in current_tags:
# first char is bold, so unbold the range
self.text.tag_remove("bold", "sel.first", "sel.last")
else:
# first char is normal, so bold the whole selection
self.text.tag_add("bold", "sel.first", "sel.last")
if __name__ == "__main__":
app=App()
app.mainloop()
将Tkinter作为tk导入
导入tkFont
类应用程序(tk.tk):
定义初始化(自):
tk.tk.\uuuuu初始化(self)
##工具栏
self.toolbar=tk.Frame()
self.bold=tk.Button(name=“toolbar”,text=“bold”,
borderwidth=1,command=self.OnBold,)
self.bold.pack(在工具栏中,side=“left”)
##GUI的主要部分
#我将使用一个框架来包含小部件和
#滚动条;那样看起来好一点。。。
text_frame=tk.frame(borderwidth=1,relief=“sunken”)
self.text=tk.text(wrap=“word”,background=“white”,
borderwidth=0,highlightthickness=0)
self.vsb=tk.Scrollbar(orient=“vertical”,borderwidth=1,
command=self.text.yview)
self.text.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(在文本框中,side=“right”,fill=“y”,expand=False)
self.text.pack(在文本框中,side=“left”,fill=“both”,expand=True)
self.toolbar.pack(side=“top”,fill=“x”)
text\u frame.pack(side=“bottom”,fill=“both”,expand=True)
#克隆文本窗口小部件字体,并将其用作某些操作的基础
#标签
bold\u font=tkFont.font(self.text,self.text.cget(“font”))
粗体字体配置(weight=“bold”)
self.text.tag\u configure(“粗体”,font=bold\u font)
self.text.tag\u configure(“拼写错误”,前台=“红色”,下划线=True)
#设置绑定以执行简单的拼写检查。这仅仅是
#键入空格时检查上一个单词。用于生产
#使用时,你需要更聪明一点
#去做吧。
self.text.bind(“,self.拼写检查)
#初始化拼写检查字典。YMMV。
self.\u words=open(“/usr/share/dict/words”).read().split(“\n”)
def拼写检查(自我、事件):
''拼写检查插入点之前的单词''
index=self.text.search(r'\s',“insert”,backwards=True,regexp=True)
如果索引==“”:
index=“1.0”
其他:
index=self.text.index(“%s+1c”%index)
word=self.text.get(索引,“插入”)
如果单词在self中。\u单词:
self.text.tag_remove(“拼写错误”,索引,“%s+%dc”%”(索引,len(word)))
其他:
self.text.tag_add(“拼写错误”,索引,“%s+%dc”%”(索引,len(word)))
def OnBold(自我):
''切换所选文本的粗体状态''
#根据第一个字符切换粗体状态
#在所选范围内。如果为粗体,请将其解除绑定。如果不是
#大胆,大胆。
当前标签=self.text.tag\u名称(“sel.first”)
如果当前_标记中为“粗体”:
#第一个字符为粗体,因此取消绑定范围
self.text.tag_remove(“粗体”、“选择第一个”、“选择最后一个”)
其他:
#第一个字符是正常的,所以整个选择都加粗
self.text.tag_add(“粗体”、“选择第一个”、“选择最后一个”)
如果名称=“\uuuuu main\uuuuuuuu”:
app=app()
app.mainloop()
好的,我愿意使用wxPython。你知道如何在wx中实现这一点吗?忘记我的wxPython建议吧。多亏了effbot,我找到了一个Tk解决方案。我希望有帮助。除了在插入期间添加标记外,还可以在运行时使用tag_add添加标记。因此,例如,您可以获取用户选择的字符范围,并将一个或多个标记应用于该文本范围。+1用于链接的附魔。好东西,哇!这就是我要找的!谢谢没有意识到这有多容易+1给你!我不断得到一个错误,说(“sel.first”)是一个坏的索引。我该如何解决这个问题?抱歉,这只是一个输入错误,但我没有为此将文本设置为粗体