【问题标题】:Python Tkinter ttk calendarPython Tkinter ttk 日历
【发布时间】:2018-01-17 09:56:53
【问题描述】:

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

它的下拉部分不起作用,我似乎无法在任何地方找到 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=..定义之前)
  • 另外,我不明白你到底想达到什么目的。你在说github.com/j4321/tkcalendar吗?
  • @j_4321 抱歉手动错误。我的问题是,我正在尝试创建一个日历下拉列表,一旦用户单击它,就会有一个小的日历下拉列表,他可以选择日期;然后选择的日期出现在空间中。我的问题是这行代码 cal=DateEntry(f2,dateformat=3,width=12, background='darkblue', foreground='white', borderwidth=4,Calendar =2018 )
  • DateEntry没有关键字Calendar,直接传日历关键字即可。

标签: python tkinter ttk tkcalendar


【解决方案1】:

更新:我已修复问题并发布了新版本的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 选项来定义条目内的初始日期。 结果如下:

【讨论】:

  • 所以我编译了代码,我得到的只是一个日期输入框,下拉菜单不可操作。我在这里错过了什么吗? python 3.4.0 中是否有错误?不确定问题是什么:(
  • @user9093127 我使用的是 python 3.6 和 tcl/tk 8.6,所以这可能是一个兼容性问题。我正在使用 appveyor 和 travis-ci 来检查旧 python(包括 python 3.4)的兼容性,但我不记得我是否为按钮编写了测试。您使用的是什么操作系统以及 tk 的哪个版本?
  • m 使用 python 3.4.0 和 tk8.6 ;windows os
  • 我已经在 linux 中使用 python 3.3.7 和 3.4.7 测试了 DateEntry 并且它可以工作。我现在没有可用的窗口,但我会尽可能做一些测试。
  • @user9093127 我想我已经确定了这个问题,它是 Windows 默认 ttk 主题,它比其他主题更不灵活,并且似乎无法正确处理向下箭头按钮。所以现在,解决方案是更改主题(我已经更新了答案),我会看看是否可以解决 Windows 主题的问题。
猜你喜欢
  • 2013-05-04
  • 2017-02-06
  • 1970-01-01
  • 2012-10-17
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 2012-12-29
相关资源
最近更新 更多