【问题标题】:Is there a way to change tkcalendar's color?有没有办法改变 tkcalendar 的颜色?
【发布时间】:2020-04-29 03:16:49
【问题描述】:

我正在尝试让 tkcalendar 与我的窗口融为一体。

import tkinter
from tkcalendar import Calendar



window = tkinter.Tk()
window.configure(background = "black")



cal = Calendar(window, background = "black" , disabledbackground = "black" , borderbackground = "black" , headersbackground = "black" , normalbackground = "black" )
cal.config(background = "black")
cal.pack()


window.mainloop()

我已通读 tkcalendar 文档并尝试通过调用小部件类的配置方法来更改所有样式元素:

cal.configure(background = "black")

;但是,我的日历仍然保持灰色,而不是融入黑色窗口背景。是否可以更改日历的实际背景颜色?

【问题讨论】:

  • 据我所知:tkcalendar 是一个小部件。在文档中它说它继承自 tkinter 的所有小部件方法。所以将 cal.config 更改为 cal.configure 就可以了
  • @yAzou 我试过了,但没有运气。它只会改变小箭头的颜色
  • 好的,我编辑你的帖子补充说你也很累调用小部件的继承方法configure...它应该对谁会阅读你的帖子有用

标签: python tkinter tkcalendar


【解决方案1】:

您这样做是正确的,除了 OSX 默认主题不支持更改背景颜色(我认为它基于图片,因此您只能更改文本颜色)。 解决方案是使用不同的 ttk 主题(例如 clam 或 alt):

import tkinter
from tkinter import ttk
from tkcalendar import Calendar

window = tkinter.Tk()
window.configure(background = "black")

style = ttk.Style(window)
style.theme_use('clam')   # change theme, you can use style.theme_names() to list themes

cal = Calendar(window, background="black", disabledbackground="black", bordercolor="black", 
               headersbackground="black", normalbackground="black", foreground='white', 
               normalforeground='white', headersforeground='white')
cal.config(background = "black")
cal.pack()

顺便说一句,“borderbackground”选项不存在,正确的名称是“bordercolor”。

【讨论】:

  • 任何改变字体颜色的选项?
  • @CoolCloud 哪个部分的字体?因为你可以单独更改月份和年份的字体颜色,表头的字体颜色,平日的,周末的,....都写在documentation
【解决方案2】:

tkcalendar 模块中的Calendar 类是subclass of ttk.Frame

class Calendar(ttk.Frame):

您必须使用styling specific to ttk 使用主题来更改其属性。

【讨论】:

  • 谢谢,但是我怎么用它来改变颜色呢?我对 tkinter 很陌生
猜你喜欢
  • 1970-01-01
  • 2020-12-10
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 2022-01-16
相关资源
最近更新 更多