You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DateEntry控件放入同一Frame后输入框无日期显示问题求助

解决DateEntry在Frame中无日期显示的问题

将多个DateEntry放入同一Frame后输入框无日期显示,即使通过textvariable调用.set()方法也无效,可通过以下方式解决:

问题分析

从你提供的代码和控件显示状态(两个DateEntry输入框均为空,仅显示下拉箭头)来看,主要可能是两个原因:

  • 绑定的StringVar变量被垃圾回收,导致控件无法读取到变量值
  • 未通过DateEntry自身的方法设置初始日期,直接操作textvariable无法触发控件更新

修复方案

1. 避免变量被垃圾回收

如果代码是在函数内编写,局部定义的StringVar可能会被Python垃圾回收机制回收。解决方法:

  • 将变量声明为全局变量
  • 若使用面向对象写法,将变量定义为类的实例属性(推荐)

2. 使用DateEntry专属方法设置初始日期

tkcalendar的DateEntry提供了set_date()方法,相比直接操作textvariable,能更可靠地触发控件更新,支持传入'today'(当前日期)或指定格式的日期字符串。

修正后的代码

# 假设status_frame、h2、blue已提前定义
import tkinter as tk
from tkcalendar import DateEntry

# Received Checkbox and date
receivedBool = tk.IntVar()
receivedBox = tk.Checkbutton(status_frame, text="Received", font=h2, variable=receivedBool, onvalue=1, offvalue=0)
receivedBox.grid(row=0, column=0, padx=(0, 10), sticky='w')
receivedBox['bg'] = blue

receivedEntry = tk.StringVar()
receivedDate = DateEntry(status_frame, font=h2, selectmode='day', textvariable=receivedEntry)
# 设置初始日期为当天
receivedDate.set_date('today')
receivedDate.grid(row=0, column=1, sticky='ew')

courseBool = tk.IntVar()
courseBox = tk.Checkbutton(status_frame, text="Course Started", font=h2, variable=courseBool, onvalue=1, offvalue=0)
courseBox.grid(row=2, column=0, padx=(0, 10), sticky='w')
courseBox['bg'] = blue

courseEntry = tk.StringVar()
courseDate = DateEntry(status_frame, font=h2, selectmode='day', textvariable=courseEntry, date_pattern='dd/MM/yyyy')
# 设置初始日期为当天
courseDate.set_date('today')
courseDate.grid(row=2, column=1, sticky='ew')

面向对象写法示例(更稳定)

import tkinter as tk
from tkcalendar import DateEntry

class MyApp:
    def __init__(self, root):
        self.root = root
        self.status_frame = tk.Frame(root, bg="blue")
        self.status_frame.pack(fill=tk.X, padx=10, pady=10)
        h2 = ("Arial", 12)
        
        # Received Checkbox and date
        self.receivedBool = tk.IntVar()
        receivedBox = tk.Checkbutton(self.status_frame, text="Received", font=h2, variable=self.receivedBool, onvalue=1, offvalue=0)
        receivedBox.grid(row=0, column=0, padx=(0, 10), sticky='w')
        receivedBox['bg'] = "blue"
        
        self.receivedEntry = tk.StringVar()
        self.receivedDate = DateEntry(self.status_frame, font=h2, selectmode='day', textvariable=self.receivedEntry)
        self.receivedDate.set_date('today')
        self.receivedDate.grid(row=0, column=1, sticky='ew')

        # Course Started Checkbox and date
        self.courseBool = tk.IntVar()
        courseBox = tk.Checkbutton(self.status_frame, text="Course Started", font=h2, variable=self.courseBool, onvalue=1, offvalue=0)
        courseBox.grid(row=2, column=0, padx=(0, 10), sticky='w')
        courseBox['bg'] = "blue"
        
        self.courseEntry = tk.StringVar()
        self.courseDate = DateEntry(self.status_frame, font=h2, selectmode='day', textvariable=self.courseEntry, date_pattern='dd/MM/yyyy')
        self.courseDate.set_date('today')
        self.courseDate.grid(row=2, column=1, sticky='ew')

if __name__ == "__main__":
    root = tk.Tk()
    app = MyApp(root)
    root.mainloop()

内容的提问来源于stack exchange,提问作者Programmer12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 13:10:58