Tiện ích văn bản Tkinter là mạnh mẽ đáng kể, nhưng bạn phải tự mình thực hiện một số tính năng nâng cao. Nó không có kiểm tra chính tả tích hợp hoặc các nút dựng sẵn cho văn bản bolding, vv, nhưng chúng khá dễ thực hiện. Tất cả các khả năng đều có trong tiện ích, bạn chỉ cần biết cách thực hiện.
Ví dụ sau cung cấp cho bạn nút để chuyển trạng thái in đậm của văn bản được đánh dấu - chọn một dải ký tự, sau đó nhấp vào nút để thêm rồi xóa thuộc tính in đậm. Nó sẽ được khá dễ dàng cho bạn để mở rộng ví dụ này cho phông chữ và màu sắc.
Kiểm tra chính tả cũng khá dễ dàng. ví dụ sau sử dụng các từ trong/usr/share/dict/words (hầu như chắc chắn không tồn tại trên Windows 7, vì vậy bạn sẽ cần phải cung cấp một danh sách các từ thích hợp) Nó khá đơn giản ở chỗ nó chỉ kiểm tra chính tả khi bạn nhấn phím cách, nhưng đó chỉ là để giữ kích thước mã của ví dụ ở mức tối thiểu. Trong thế giới thực, bạn sẽ muốn thông minh hơn một chút khi bạn kiểm tra chính tả.
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()
Câu hỏi bây giờ dường như không yêu cầu nguồn cấp dữ liệu, thay vì thực hiện như tất cả các câu hỏi khác về SO cũng có. – Trilarion
Tôi bị bối rối bởi việc đóng cửa do không có chủ đề. Đây không phải là yêu cầu để giúp tìm một nguồn tài nguyên hoặc công cụ cho mỗi se, nó hỏi làm thế nào một cái gì đó có thể được thực hiện. Phải thừa nhận rằng nó hơi rộng, nhưng câu trả lời được chấp nhận không liên quan đến các công cụ hoặc thư viện bên ngoài, nó chỉ mô tả cách sử dụng các tính năng hiện có. Tôi nghĩ câu hỏi này xứng đáng được mở lại. –