Python Tkinter ttk 日历

use*_*127 3 python tkinter ttk tkcalendar

我正在尝试为日期条目创建下拉日历。下面是我的代码的一部分:

它的下拉部分不起作用,我似乎无法在DateEntry()任何地方找到ttk 日历的语法来包含日历小部件选项!

#creating the frame 
from tkinter import *
from tkcalendar import *

root = Tk()

f1=Frame(root,width=1500,height=100,relief=SUNKEN,bd=4,bg='light steel blue')
f1.pack(side=TOP)
f2=Frame(root,width=1500,height=550,relief=SUNKEN,bd=4,bg='white')
f2.pack()
f3=Frame(root,width=1600,height=100,relief=SUNKEN,bd=4,bg='white')
f3.pack(side=BOTTOM)


#Creating the date column
l4=Label(f2,text='DATE',font=('tahoma',20,'bold'),fg='black',anchor='w')
l4.grid(row=0,column=3)

cal=DateEntry(f2,dateformat=3,width=12, background='darkblue',
                    foreground='white', borderwidth=4,Calendar =2018)
cal.grid(row=1,column=3,sticky='nsew')
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像这样: 截屏

j_4*_*321 5

更新:我已经解决了这个问题并发布了tkcalendar的新版本。

编辑:问题是在 Windows 中,单击向下箭头按钮时下拉菜单不会打开。它似乎来自 Windows 的默认 ttk 主题,因为它适用于其他主题。因此,解决方法是切换主题并使用 'clam' 例如('alt' 也应该工作)。同时,我会研究它,看看我是否可以修复DateEntry其他主题并发布新版本(https://github.com/j4321/tkcalendar/issues/3)。

我不确定你想用 来实现什么DateEntry,但如果你的目标是让它看起来像图片中的那样,可以通过以下方式完成:

import tkinter as tk
from tkinter import ttk
from tkcalendar import DateEntry
from datetime import date

root = tk.Tk()
# change ttk theme to 'clam' to fix issue with downarrow button
style = ttk.Style(root)
style.theme_use('clam')

class MyDateEntry(DateEntry):
    def __init__(self, master=None, **kw):
        DateEntry.__init__(self, master=None, **kw)
        # add black border around drop-down calendar
        self._top_cal.configure(bg='black', bd=1)
        # add label displaying today's date below
        tk.Label(self._top_cal, bg='gray90', anchor='w',
                 text='Today: %s' % date.today().strftime('%x')).pack(fill='x')

# create the entry and configure the calendar colors
de = MyDateEntry(root, year=2016, month=9, day=6,
                 selectbackground='gray80',
                 selectforeground='black',
                 normalbackground='white',
                 normalforeground='black',
                 background='gray90',
                 foreground='black',
                 bordercolor='gray90',
                 othermonthforeground='gray50',
                 othermonthbackground='white',
                 othermonthweforeground='gray50',
                 othermonthwebackground='white',
                 weekendbackground='white',
                 weekendforeground='black',
                 headersbackground='white',
                 headersforeground='gray70')
de.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

我创建了一个类继承自DateEntry同今天的日历下方日期添加标签和创建一个围绕下拉黑色边框(self._top_calToplevel包含日历)。

然后,我创建了一个实例,MyDateEntry并带有使其看起来像图片所需的所有日历选项。此外,我使用year, month,day选项来定义条目内的初始日期。结果如下:

截屏