Python Tkinter ttk 日历

3
我正在尝试创建一个下拉式日期选择器。 以下是我的代码的一部分:
其中的下拉式部分无法工作,我似乎找不到ttk日历DateEntry()的语法,以包括日历小部件选项!
#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')

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

您提供的代码存在问题,您在定义root之前在f1=..中使用了它。 - j_4321
另外,我不太明白您想要实现什么。您是在谈论 https://github.com/j4321/tkcalendar 吗? - j_4321
@j_4321 抱歉,这是手动错误。我的问题是,我正在尝试创建一个日历下拉菜单,一旦用户点击它,就会出现一个小的日历下拉菜单,他可以选择日期;然后所选日期将出现在空间中。我的问题出在这行代码上: cal=DateEntry(f2,dateformat=3,width=12, background='darkblue', foreground='white', borderwidth=4,Calendar =2018) - user9093127
@j_4321是的,我已经查看了页面,不确定如何直接传递日历关键词。 - user9093127
我不明白问题出在哪里。除了显示今天日期的那一行之外,DateEntry 的结果与图片中的非常相似,当用户点击箭头按钮时,日历会出现。 - j_4321
显示剩余7条评论
1个回答

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

我创建了一个继承自DateEntry的类,以在日历下添加带有今天日期标签,并在下拉菜单周围创建黑色边框(self._top_cal是包含日历的Toplevel)。
然后,我创建了MyDateEntry实例,并使用所有必要的日历选项使其看起来像图片。此外,我使用yearmonthday选项定义了输入框内的初始日期。以下是结果:

screenshot


我已经编译了代码,但是我只得到了一个带有不可操作的下拉日期输入框。我错过了什么吗?Python 3.4.0中是否存在错误?不确定问题出在哪里:( - user9093127
@user9093127 我正在使用Python 3.6和tcl/tk 8.6,所以这可能是一个兼容性问题。我正在使用appveyor和travis-ci来检查旧版本的Python(包括Python 3.4)的兼容性,但我不记得是否编写了一个按钮测试。您使用的操作系统是什么,使用的tk版本是哪个? - j_4321
我正在使用Python 3.4.0和Tk8.6;操作系统为Windows。 - user9093127
@user9093127 我认为我已经找到了问题所在,那就是Windows默认的ttk主题不如其他主题灵活,并且似乎无法正确处理下箭头按钮。因此,暂时的解决方案是更改主题(我已经更新了答案),并且我会看看是否可以修复Windows主题的问题。 - j_4321
@user9093127,我已经在Windows上修复了这个问题并发布了一个新版本。 - j_4321
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接