英文:
Tkcalendar configure DateEntry widget
问题
Is there any way to configure the DateEntry widget itself as you would with a normal entry widget (not the drop-down calendar)? For example, change the font, relief, or background, etc...
I've tried the following:
myDateEntry.config(background='red')
but I just get:
AttributeError: 'DateEntry' object has no attribute 'background'
When I define the DateEntry widget, I can do the following:
myDateEntry = DateEntry(root, background='red')
which doesn't cause an error but only changes the background of the drop-down calendar.
英文:
Is there any way to configure the DateEntry widget itself as you would with a normal entry widget (not the drop down calendar)? For example change the font, relief or background etc...
I've tried the following:
myDateEntry.config(background='red')
but i just get:
AttributeError: 'DateEntry' object has no attribute 'background'
When i define the DateEntry widget i can do the following:
myDateEntry=DateEntry(root,background='red')
which doesn't cause an error, but only changes the background of the drop down calendar.
答案1
得分: 2
DateEntry
组件基于ttk.Entry
,而不是tk.Entry
,所以您必须使用样式来更改其外观。这在文档中有解释:https://tkcalendar.readthedocs.io/en/stable/howtos.html#widget-styling
与ttk.Entry
类似,如果您想要红色背景,您需要将样式的fieldbackground
选项设置为'red',只是要更改的样式是'DateEntry'而不是'TEntry':
import tkinter as tk
from tkinter import ttk
from tkcalendar import DateEntry
root = tk.Tk()
style = ttk.Style(root)
# 创建自定义的DateEntry样式,带有红色背景
style.configure('my.DateEntry', fieldbackground='red')
# 使用自定义样式创建DateEntry
dateentry = DateEntry(root, style='my.DateEntry')
dateentry.pack()
root.mainloop()
注意: 并非所有ttk主题都允许更改小部件的fieldbackground
,特别是Windows默认主题。因此,要能够更改它,首先需要使用style.theme_use('clam')
更改主题,以使用例如'clam'主题。
英文:
The DateEntry
widget is based on a ttk.Entry
, not a tk.Entry
, so you have to use a style to change its appearance. This is explained in the documentation: https://tkcalendar.readthedocs.io/en/stable/howtos.html#widget-styling
Like for a ttk.Entry
, if you want a red background, you need to set the fieldbackground
option of the style to 'red', except that the style to change is 'DateEntry' instead of 'TEntry':
import tkinter as tk
from tkinter import ttk
from tkcalendar import DateEntry
root = tk.Tk()
style = ttk.Style(root)
# create custom DateEntry style with red background
style.configure('my.DateEntry', fieldbackground='red')
# create DateEntry using the custom style
dateentry = DateEntry(root, style='my.DateEntry')
dateentry.pack()
root.mainloop()
Note: Not all ttk themes allow to change the fieldbackground
of the widgets, especially Windows default theme. To be able to change it therefore the theme needs to be changed first with style.theme_use('clam')
to use, for instance, the 'clam' theme.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论