如何在Python的TkTable中更改活动单元格的文本颜色?

4 投票
1 回答
1297 浏览
提问于 2025-04-17 19:19

我在用Python的tkinter模块制作一个应用程序,里面用到了tktable。

当我选择一个单元格并在里面输入文字时,文字的颜色是白色的,这样看起来很费劲。请问我怎么能把这个颜色改成黑色呢?

我已经尝试过改变“前景色”,但似乎没有什么效果。

self.table = tktable.Table(self, rows=5, cols=5, multiline=0, font=("Helvetica", 10), foreground='Red', background='White', cache=True, colstretchmode='all')

self.table.grid(row=0, column=1, padx=10, pady=10, sticky='e,w')

给那些找到这个讨论的人一个完整的解决方案:

import Tkinter as tk
import tktable

class App(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        self.Main()
        self.grid()

    def Main(self):
        self.table = tktable.Table(self, rows=5, cols=5, multiline=0, font=("Helvetica", 10), foreground='Black', background='White', cache=True, colstretchmode='all')
        self.table.tag_configure('active', foreground='black') # <<<ADDED LINE/ SOLUTION
        self.table.grid(row=0, column=0, padx=10, pady=10, sticky='e,w')

if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    app.mainloop()

1 个回答

5

当一个单元格被选中进行编辑时,'active'标签就会生效。

你可以试试下面的代码:

self.table.tag_configure('active', foreground='black')

撰写回答