Python Tkinter ttk日历

Python Tkinter ttk日历,python,tkinter,ttk,tkcalendar,Python,Tkinter,Ttk,Tkcalendar,我正在尝试为日期条目创建下拉日历。 下面是我代码的一部分: 它的下拉部分不起作用,我似乎找不到ttk calendar anywhere的DateEntry()的语法来包含calendar小部件选项 #creating the frame from tkinter import * from tkcalendar import * root = Tk() f1=Frame(root,width=1500,height=100,relief=SUNKEN,bd=4,bg='light ste

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

它的下拉部分不起作用,我似乎找不到ttk calendar anywhere的
DateEntry()
的语法来包含calendar小部件选项

#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')
我希望它看起来像这样:

更新:我已修复该问题并发布了新版本的

编辑:问题在于,在Windows中,单击向下箭头按钮时,下拉列表不会打开。它似乎来自Windows的默认ttk主题,因为它与其他主题一起工作。因此,解决方法是切换主题,例如使用“clam”('alt'也可以)。同时,我将研究它,看看是否可以修复其他主题的
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()
我创建了一个继承自
DateEntry
的类,以便在日历下方添加带有今天日期的标签,并在下拉列表周围创建一个黑色边框(
self.\u top\u cal
是包含日历的
Toplevel

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


您提供的代码有问题,您正在使用
root
(在定义它之前在
f1=..
中)此外,我不清楚您想要实现什么。你在说什么?@j_4321为手动错误道歉。我的问题是,我试图创建一个日历下拉列表,一旦用户点击它,就会有一个小的日历下拉列表,他可以选择日期;所选日期随后出现在空白处。我的问题是这行代码cal=DateEntry(f2,dateformat=3,宽度=12,背景为深色,前景为白色,边框宽度=4,日历为2018)。关于语法,请检查
DateEntry
没有关键字
Calendar
,直接传递日历关键字。所以我编译了代码,我得到的只是一个日期输入框,下拉列表为“不可操作”。我是不是遗漏了什么?python 3.4.0中是否存在bug??不确定问题是什么:(@user9093127我正在使用python 3.6和tcl/tk 8.6,因此这可能是一个兼容性问题。我正在使用appveyor和travis ci检查旧python(包括python 3.4)的兼容性)但我不记得我是否为按钮编写了测试。您使用的是什么操作系统以及tk的哪个版本?我使用的是python 3.4.0和tk8.6;windows osI在linux中使用python 3.3.7和3.4.7测试了
DateEntry
,它运行正常。我现在没有可用的windows,但我会在可能的情况下进行一些测试。@user9093127我已经确定了这个问题,这是Windows默认ttk主题,它没有其他主题灵活,而且似乎无法正确处理向下箭头按钮。因此,目前的解决方案是更改主题(我已更新了答案),我将看看是否可以解决Windows主题的问题。