English 中文(简体)
是否有办法在案文中添加一字,以示意图?
原标题:Is there a way in tkinter to italicize a single word in a text widget?

我正试图用一种文字来显示某些非许可特性,其行文与斜体相同。 例如,我要说,“这是mytext widget”全线。

As far as I know, you can only change the font formatting of the entire widget, not individual words. I ve looked online quite a bit, but could not answer this question. I am wondering if this is possible, or if I should try a different method from using text widgets.

最佳回答

You can apply a tag to a range of characters in a text widget. Each tag can be configured with a variety of styles and a custom font. Some styles can be applied directly to the tag, some need to be applied to a font that is associated with the tag. Italics is one such style that must be applied to a font.

import tkinter as tk
from tkinter.font import Font

root = tk.Tk()

normal_font = Font(family="helvetica", size=20)
italic_font = Font(family="helvetica", size=20, slant="italic")

text = tk.Text(root, wrap="none", width=40, height=10, font=normal_font)
text.pack(fill="both", expand=True)

text.tag_configure("italics", font=italic_font)

text.insert("end", "This is ", "", "my", "italics", " text widget")

root.mainloop()

注:insert 这种方法通常用于在某一时间插入一个插图,但实际上以texttag/em>、>>>>>>>>>、......(最后标签可以省略)。

In the above example, we insert the whole string using a single call to insert, but notice how we apply a null tag to some parts, and the tag "italics" to the word "my". That one call to insert does exactly the same thing as these three lines, but in a more concise format:

text.insert("end", "This is ")
text.insert("end", "my", "italics")
text.insert("end", " text widget")

这样做的另一个方式是一劳永逸地插入整个插图,然后在一份单独声明中添加标记:

text.insert("end", "This is my text widget")
text.tag_add("italics", "1.8", "1.9")

“

问题回答

不幸的是,阴.的阴.有直接接触。 工作方式可以是将案文的行文分为不同的内容,对每一条目适用不同的风格。 这可能会产生类似于预期的效果。

例:

import tkinter as tk

root = tk.Tk()
root.geometry("400x100")

text_widget = tk.Text(root, wrap="none", height=1)
text_widget.pack()

text_widget.insert(tk.END, "This is ")
text_widget.tag_add("italic", "1.0", "1.4")
text_widget.tag_config("italic", font=("Times", 12, "italic"))

text_widget_italic = tk.Text(root, wrap="none", height=1)
text_widget_italic.pack()

text_widget_italic.insert(tk.END, "my")
text_widget_italic.tag_add("italic", "1.0", "1.2")
text_widget_italic.tag_config("italic", font=("Times", 12, "italic"))

text_widget.insert(tk.END, " text widget")
text_widget.tag_add("normal", "1.5", "1.end")
text_widget.tag_config("normal", font=("Times", 12))

root.mainloop()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...