首页 > 解决方案 > 位图“颜色”未定义

问题描述

我的代码:

import tkinter as tk

win = tk.Tk()

txt = tk.Text(win, width = 50, height = 20)
txt.pack()

txt.insert('insert', 'Hello1\n')
txt.insert('insert', 'Hello2')
txt.tag_add('format1', '1.0', '1.5')
txt.tag_configure('format1', bg = 'blue')

win.mainloop()

错误:

输出3

上面的代码与下面的代码类似,因为我以它为例。

示例代码:

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_configure('here', background = 'yellow', foreground = 'blue')
text.tag_configure('start', background = 'black', foreground = 'green')

win.mainloop()

来源:https ://www.tutorialspoint.com/python/tk_text.htm

问题:

我的代码中的错误在哪里?就我而言,bg选项会产生错误,因为如果我们改变它的值,比如说红色,我们会得到:

输出2

标签: pythontkinter

解决方案


如果我们在我的代码bg中更改为,我们会得到:background

输出3

请注意,尽管相似tk.Text,但 中的选项与 中的选项不同。正如jasonharper在评论中所说,不是in而是 for的缩写。.tag_configure()bgbackground.tag_configure()bgstipple

因此,我的消息来源建议是错误的:

输出4

结束语:

和 也一样bdfg因为它们不是borderwidth和的缩写foreground,与 . 不同 tk.Text。如果您像我一样不知道哪些可能会产生错误,我建议在命名选项时要明确。


推荐阅读