打造日历记事本总是出错,几个问题:tkinter的Text文本获取出错,事件<FocusOut>相应循序也不对

打造日历记事本总是出错,几个问题:tkinter的Text文本获取出错,事件<FocusOut>相应循序也不对

打造日历记事本总是出错,几个问题:tkinter的Text文本获取出错,事件<FocusOut>相应循序也不对
from tkinter import *

import tkinter.font as tkFont
import calendar
from datetime import date
from tkinter import filedialog

class DateCtrl(Frame):

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

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

self.master = master

self.changed = False

#self.master.size = (800,1200)

#self.master.resizable(False, False)

self.master.resizable(True, True)

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 FileAdd (self):

pass

#file_path = filedialog.askopenfilename()

self.UpdateUI()

def FileDel (self):

pass

self.UpdateUI()

def NoteChanged (self):

self.changed = True

print(self.note.get(1.0,END))

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.note.destroy()

del(self.note)

self.fileAddBt.destroy()

del(self.fileAddBt)

'''

boldFont = tkFont.Font (size = 20, weight = "bold")

self.backwardBt = Button(text='<',command=self.MonthBack,font = boldFont).grid(row=0, column=0, sticky=W+E+N+S)

self.YMBtn = Button(text='%d-%d'%(self.date.year,self.date.month),command=lambda sf=self:print(sf.date),font = boldFont).grid(row=0, column=1,columnspan = 5, sticky=W+E+N+S)

self.forwardBt = Button(text='>',command=self.MonthFoeward,font = boldFont).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 = 'red'

if col == 6:

bkcolour = 'yellow'

'''

if dayt == self.date.day:

bkcolour = 'yellow'

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

#self.note = Text(height=5).grid(row=row+1, column=0,columnspan = 2, sticky=W+N+S)

self.note = Text(height=5)

self.note.grid(row=row+1, column=0,columnspan = 2, sticky=W+N+S)

self.note.bind("<FocusOut>",self.NoteChanged) # 绑定失去光标焦点事件

self.fileAddBt = Button(text='document',command=self.FileAdd,font = boldFont).grid(row=row+1, column=4, sticky=W+E+N+S)

self.ClearBt = Button(text='FileDel',command=self.FileDel,font = boldFont).grid(row=row+1, column=5, sticky=W+E+N+S)

self.exit = Button(text='exit',command=lambda : root.destroy(),font = boldFont).grid(row=row+1, column=6, sticky=W+E+N+S)

print(str(row))

print(self.date.strftime( '%Y-%m-%d' ))

def rpday(self,dt):

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

self.UpdateUI()

if name == '__main__':

root = Tk()

root.geometry('1100x800+200+100')

#root.minsize(800, 480)

for i in range(2,8) :

root.rowconfigure(i, weight=1)

for i in range(7) :

root.columnconfigure(i, weight=1)

mainfram = DateCtrl(root)

tdt = date.today()

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

root.mainloop()

请高手指点!


回答:

from tkinter import *

import tkinter.font as tkFont
import calendar
from datetime import date
from tkinter import filedialog

class DateCtrl(Frame):

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

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

self.master = master

self.changed = False

#self.master.size = (800,1200)

#self.master.resizable(False, False)

self.master.resizable(True, True)

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 FileAdd (self):

pass

#file_path = filedialog.askopenfilename()

self.UpdateUI()

def FileDel (self):

pass

self.UpdateUI()

def NoteChanged (self):

self.changed = True

print(self.note.get(1.0,END))

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.note.destroy()

del(self.note)

self.fileAddBt.destroy()

del(self.fileAddBt)

'''

boldFont = tkFont.Font (size = 20, weight = "bold")

self.backwardBt = Button(text='<',command=self.MonthBack,font = boldFont).grid(row=0, column=0, sticky=W+E+N+S)

self.YMBtn = Button(text='%d-%d'%(self.date.year,self.date.month),command=lambda sf=self:print(sf.date),font = boldFont).grid(row=0, column=1,columnspan = 2, sticky=W+E+N+S)

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

self.fileAddBt = Button(text='document',command=self.FileAdd,font = boldFont).grid(row=0, column=4, sticky=W+E+N+S)

self.ClearBt = Button(text='FileDel',command=self.FileDel,font = boldFont).grid(row=0, column=5, sticky=W+E+N+S)

self.exit = Button(text='exit',command=lambda : root.destroy(),font = boldFont).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()

tttday = 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 = 'red'

if col == 6:

bkcolour = 'yellow'

'''

if dayt == self.date.day:

bkcolour = 'yellow'

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=LabelFrame(height=100, width=200, text='%d'%dayt,bg=bkcolour)

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

t_2 =Text(bt, height=3)

t_2.bind("<FocusIn>",lambda sf=self,dt=dayt:sf.rpday(dt)) # 绑定光标焦点事件

t_2.pack(side=TOP,expand=YES,fill=BOTH)

lab_2 =Label(bt,text=" ").pack(side=BOTTOM,expand=YES,fill=BOTH)

#self.dayid.append(t_2)

self.dayid.append(bt)

col+=1

row+=1

col=0

print(str(dayt))

print(self.date.strftime( '%Y-%m-%d' ))

def rpday(self,dt):

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

self.UpdateUI()

if name == '__main__':

root = Tk()

root.geometry('1100x800+200+100')

#root.minsize(800, 480)

for i in range(2,8) :

root.rowconfigure(i, weight=1)

for i in range(7) :

root.columnconfigure(i, weight=1)

mainfram = DateCtrl(root)

tdt = date.today()

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

root.mainloop()

改进版本,代码没有完成,还有小问题

以上是 打造日历记事本总是出错,几个问题:tkinter的Text文本获取出错,事件&lt;FocusOut&gt;相应循序也不对 的全部内容, 来源链接: utcz.com/p/937696.html

回到顶部