Tkcalendar 配置 DateEntry 小部件

huangapple go评论78阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月6日 16:37:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定