You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Tkinter编写的文本编辑器修改背景颜色?

如何修改Tkinter文本编辑器的背景颜色?

要修改Tkinter文本编辑器的背景色,只需在创建Text组件时添加bg(或等价的background)参数即可。如果需要让文字和背景对比更清晰,还可以通过fg(或foreground)参数调整文字颜色。

如果希望界面风格统一,主窗口、框架和滚动条的背景色也可以用同样的方式修改。

修改后的完整代码

from tkinter import *
import tkinter as tk

root = Tk()
root.title('Flax')
root.iconbitmap('E:\\editor.ico')
root.geometry('1200x660')
# 修改主窗口背景色
root.config(bg="#e8e8e8")

# Main Frame
my_frame = Frame(root, bg="#e8e8e8")  # 修改框架背景色
my_frame.pack(pady=5)

# ScrollBar
scroll = Scrollbar(my_frame, bg="#e8e8e8")  # 修改滚动条背景色
scroll.pack(side=RIGHT, fill=Y)

# Text Box
text = Text(
    my_frame, 
    width=98, 
    height=25, 
    font=("Helvetica", 13), 
    selectbackground="grey", 
    selectforeground="white", 
    undo=True,
    bg="#f5f5f5",  # 修改文本框背景色
    fg="#333333"   # 修改文本框文字颜色
)          
text.pack()

scroll.config(command=text.yview)

root.mainloop()

补充说明

  • bg参数支持颜色名称(如lightblue、white)或十六进制颜色码(如#f5f5f5)
  • 若不需要统一整体背景,仅修改Text组件的bg参数即可满足需求
  • 滚动条的背景色也可通过bg参数调整,保证界面风格协调

内容的提问来源于stack exchange,提问作者Odysseus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 21:57:40