Python Tkinter ttk日历

您所在的位置:网站首页 ttkpython Python Tkinter ttk日历

Python Tkinter ttk日历

2023-04-29 09:13| 来源: 网络整理| 查看: 265

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

它的下拉部分无法正常工作,我似乎在任何地方都无法找到ttk日历的DateEntry()的语法来包括日历小部件选项!

123456789101112131415161718192021#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) 有关语法,请检查pypi.python.org/pypi/tkcalendar#Documentation DateEntry没有关键字Calendar,仅直接传递日历关键字。 @ j_4321yes香港专业教育学院浏览过该页面,不确定我如何进行此操作"仅直接传递日历关键字" 您能告诉我您想传递给窗口小部件什么选项吗? 我希望它看起来像这样:cdn.ablebits.com/_img-blog/excel-calendar/ @ j_4321我的评论仅获取当前日期,但我似乎无法使日历显示在其下方 "直接"表示您可以使用与"日历"中相同的关键字-即。 Calendar( ..., year=2018)和DateEntry( ... year=2018) 我不明白问题出在哪里。除了显示今天的日期的行外,DateEntry给出的结果与图片中的结果非常相似,当用户单击箭头按钮时,日历出现。 @ j_4321正是问题所在,这是不可单击的箭头 @furas yep尝试使用cal5 = DateEntry(f2,dateformat = 3,width = 12,background = darkblue,front = white,borderwidth = 4);问题是下拉列表不可操作

更新:我已解决此问题,并发布了新版本的tkcalendar。

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

我不确定您要使用DateEntry到底要实现什么,但是如果您的目标是使其看起来像图片中的那个,则可以通过以下方式实现:

1234567891011121314151617181920212223242526272829303132333435363738import 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的实例,并带有使它看起来像图片所需的所有日历选项。另外,我使用了year,month,day选项来定义条目中的初始日期。 结果如下:

相关讨论 因此,我已经编译了代码,而我得到的只是一个日期输入框,下拉列表不可操作。我在这里想念什么吗? 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,它可以工作。我现在没有可用的Windows,但是我会做一些测试。 @ user9093127我想我已经确定了问题,这是Windows默认的ttk主题,它比其他主题更不灵活,而且似乎无法正确处理向下箭头按钮。因此,目前,解决方案是更改主题(我已经更新了答案),我将看看是否可以解决Windows主题的问题。 @ user9093127我已经解决了Windows上的问题并发布了新版本。 @ j_4321:tkcalendar,DateEntry对我不起作用。尝试使用pip安装tkcalendar进行安装时,抛出错误("找不到与tkcalendar匹配的发行版") @GarimaTiwari您使用的是哪个OS和python版本? @ j_4321:我在Windows7上使用Python 3.6.1 @GarimaTiwari我现在无法访问Windows进行测试,但是pip在Linux上与python3.6一起适用于我。请在github.com/j4321/tkcalendar/issues上打开错误报告,然后看看我能做什么。 @ j_4321:我终于可以安装tkcalendar了!对于我们可以较早下载的软件包而言,似乎存在一些限制。 @ j_4321:但是,我还有关于DateEntry的另一个问题-您能看看这个吗?非常感谢stackoverflow.com/questions/50625818/



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3