Python 使用迭代时在tkinter textarea小部件中设置多个字体
我目前正在tkinter中创建一个项目,涉及到textarea小部件的使用。 我想在文本区域小部件中设置多个字体,但唯一的问题是,我使用的是迭代,所以我无法找到它 与我有关的代码:Python 使用迭代时在tkinter textarea小部件中设置多个字体,python,user-interface,tkinter,tk,Python,User Interface,Tkinter,Tk,我目前正在tkinter中创建一个项目,涉及到textarea小部件的使用。 我想在文本区域小部件中设置多个字体,但唯一的问题是,我使用的是迭代,所以我无法找到它 与我有关的代码: for i in range(len(news_list)): txtarea.configure(font=("Bahnschrift", 20) ) txtarea.insert(END, news_list[i]["title"] + &q
for i in range(len(news_list)):
txtarea.configure(font=("Bahnschrift", 20) )
txtarea.insert(END, news_list[i]["title"] + "\n\n" )
txtarea.configure(font=("Bahnschrift", 20) )
txtarea.insert(END, "Description:" + str(news_list[i]["description"]) + "\n\n" )
txtarea.insert(END, "Read More at:" + str(news_list[i]["url"]) + "\n\n" )
txtarea.insert(END , "--------------------------------- " + "\n")
期望输出:
看到不同的行有不同的字体了吗
实际产出:
谁能帮我一下吗?另外,由于我是Stackoverflow的新手,当您有固定的文本要显示时,您可以使用标记来指出我犯的错误。例如,运行以下代码-
import tkinter as tk
root = tk.Tk()
tbox = tk.Text(root, height = 5, width = 20, font = ('Calibri', 15))
tbox.pack()
# adding text to the text box
tbox.insert(tk.END, "Hello\nWe are exited for \nspace exploration.")
#adding tags
tbox.tag_add('Tag1', '3.0', '3.17')
tbox.tag_config('Tag1', font = ('Calibri', 20, 'bold'))
root.mainloop()
注意:创建标记的语法是-
<text box object>.tag_add(<tag name>, <starting character>, <ending character>)
.tag\u添加(,)
如果要显示固定文本,则可以使用标记。例如,运行以下代码-
import tkinter as tk
root = tk.Tk()
tbox = tk.Text(root, height = 5, width = 20, font = ('Calibri', 15))
tbox.pack()
# adding text to the text box
tbox.insert(tk.END, "Hello\nWe are exited for \nspace exploration.")
#adding tags
tbox.tag_add('Tag1', '3.0', '3.17')
tbox.tag_config('Tag1', font = ('Calibri', 20, 'bold'))
root.mainloop()
注意:创建标记的语法是-
<text box object>.tag_add(<tag name>, <starting character>, <ending character>)
.tag\u添加(,)
为每段文本定义标签
:
import tkinter as tk
TEXT = [("mountain", 'title'),
("[maʊntən]", "monospaces"),
("a large natural elevation of the earth's surface rising abruptly from the surrounding level", 'normal')]
root = tk.Tk()
t = tk.Text(root)
t.pack()
t.tag_configure("title", font=("Bahnschrift bold", 20))
t.tag_configure("monospaces", font=("Lucida", 12))
t.tag_configure("normal", font=("Arial", 14))
for text in TEXT:
t.insert(tk.END, f'{text[0]}\n', text[1])
tk.mainloop()
输出:
为每段文本定义
标签
:
import tkinter as tk
TEXT = [("mountain", 'title'),
("[maʊntən]", "monospaces"),
("a large natural elevation of the earth's surface rising abruptly from the surrounding level", 'normal')]
root = tk.Tk()
t = tk.Text(root)
t.pack()
t.tag_configure("title", font=("Bahnschrift bold", 20))
t.tag_configure("monospaces", font=("Lucida", 12))
t.tag_configure("normal", font=("Arial", 14))
for text in TEXT:
t.insert(tk.END, f'{text[0]}\n', text[1])
tk.mainloop()
输出:
这是可行的,但您能告诉我在我的情况下如何修改它吗?什么是
我的情况
?我的意思是,正如我在上面的代码中提到的,我正在使用API,并使用迭代来输出数据。在我的情况下,我不可能知道数据将是什么,所以你能帮我吗?这是可行的,但你能告诉我如何在我的情况下修改它吗?什么是我的情况?我的意思是,正如我在上面的代码中提到的,我正在使用API,并使用迭代来输出数据。在我的情况下,我不可能知道数据是什么,所以你能帮我吗?