Is there a way to change tkcalendar's color?

ぐ巨炮叔叔 提交于 2021-01-27 12:00:59

问题


I'm trying to make tkcalendar blend in with my window.

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()

I've read through the tkcalendar documentation and tried changing all the style elements by calling the configure method of widget class :

cal.configure(background = "black")

; however, my calendar still remains gray instead of blending into the black window background. Is it possible to change the actual background color of the calendar?


回答1:


You are doing it the right way, except that OSX default theme does not support changing background colors (it is based on pictures I think so you can only change the text color). The solution is to use a different ttk theme (e.g. clam or 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()

By the way, the option 'borderbackground' does not exists, the correct name is 'bordercolor'.




回答2:


The Calendar class in tkcalendar module is a subclass of ttk.Frame.

class Calendar(ttk.Frame):

You must use the styling specific to ttk that uses themes to alter its attributes.



来源:https://stackoverflow.com/questions/61493630/is-there-a-way-to-change-tkcalendars-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!