python选择日期控件_Python3 自己写了个DateCtrl日期控件 | 学步园

本文介绍了一位开发者如何在Python中自制一个类似于VC中CDateTimeCtrl的日期选择控件DateCtrl。该控件允许用户通过界面选择日期,并提供了前后翻月的功能。控件的代码包括创建日历布局、设置日期、获取日期以及更新界面等方法。在主函数中,创建了Tkinter窗口并实例化DateCtrl,展示了如何使用这个控件并设置初始日期。
摘要由CSDN通过智能技术生成

我想在Python中找个像是VC中CDateTimeCtrl控件一样选择日期的控件没找着,谁知到告诉我啊

后来我自己写了一个,代码如下:

--------------------------DateCtrl.py----------------------------

#作者 常亮

#时间:2011年4月30日15:48:47

from tkinter import *

import calendar

from datetime import date

class DateCtrl(Frame):

def __init__ (self, master=None, cnf={}, **kw):

Frame.__init__( self, bg='lightgray')

self.master = master

self.master.size = (100,150)

self.master.resizable(False, False)

self.date = date.today()

self.master.title('我的日历')

self.master.rowconfigure( 0, weight = 1 )

self.master.columnconfigure( 0, weight = 1 )

self.grid( sticky = W+E+N+W )

self.dayid = []

self.UpdateUI()

def SetDate(self,date):

self.date = date

self.UpdateUI()

def GetDate(self):

return self.date

def MonthBack (self):

if date == date.min:

return

if self.date.month == 1:

self.date = self.date.replace(year=self.date.year-1, month=12)

else:

if self.date.day > calendar.monthrange(self.date.year,self.date.month-1)[1]:

self.date = self.date.replace(month=self.date.month-1,day=calendar.monthrange(self.date.year,self.date.month-1)[1])

else:

self.date = self.date.replace(month=self.date.month-1)

self.UpdateUI()

def MonthFoeward (self):

if date == date.max:

return

if self.date.month == 12:

self.date = self.date.replace(year=self.date.year+1,month=1)

else:

if self.date.day > calendar.monthrange(self.date.year,self.date.month+1)[1]:

self.date = self.date.replace(month=self.date.month+1,day=calendar.monthrange(self.date.year,self.date.month+1)[1])

else:

self.date = self.date.replace(month=self.date.month+1)

self.UpdateUI()

def UpdateUI (self):

lendayid = len(self.dayid)

for i in range(lendayid):

self.dayid[lendayid-i-1].destroy()

del(self.dayid[lendayid-i-1])

self.backwardBt = Button(text='

self.YMBtn = Button(text='%d-%d'%(self.date.year,self.date.month),command=lambda sf=self:print(sf.date))/

.grid(row=0, column=1,columnspan = 5, sticky=W+E+N+S)

self.forwardBt = Button(text='>',command=self.MonthFoeward).grid(row=0, column=6, sticky=W+E+N+S)

col = 0

for wk in ['一','二','三','四','五','六','日']:

Label(text=wk).grid(row=1,column=col,sticky=W+E+N+S)

col += 1

row = 2

col = 0

today = date.today()

for weekday in calendar.monthcalendar(self.date.year,self.date.month):

for dayt in weekday:

if dayt == 0:

col+=1

continue

bkcolour = 'lightgray'

if col == 5:

bkcolour = 'green'

if col == 6:

bkcolour = 'blue'

if dayt == self.date.day:

bkcolour = 'red'

tdrelief = FLAT

if self.date.year==today.year/

and self.date.month==today.month/

and dayt == today.day:

tdrelief = GROOVE

bt = Button(self.master,text='%d'%dayt,relief=tdrelief,bg=bkcolour,

command=lambda sf=self,dt=dayt:sf.rpday(dt))

bt.grid(row=row, column=col, sticky=W+E+N+S)

self.dayid.append(bt)

col+=1

row+=1

col=0

def rpday(self,dt):

self.date=self.date.replace(day=dt)

self.UpdateUI()

if __name__ == '__main__':

root = Tk()

mainfram = DateCtrl(root)

tdt = date.today()

mainfram.SetDate(tdt.replace(day=3))#试试重置日期

root.mainloop()

-----------------------------------------------------------------------------

点击年月会打印当前控件的日期

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值