问答

打造日历记事本总是出错,几个问题:tkinter的Text文本获取出错

作者:admin 2021-10-26 我要评论

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

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

打造日历记事本总是出错,几个问题: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()

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

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 打造日历记事本总是出错,几个问题:tk

    打造日历记事本总是出错,几个问题:tk

  • php循环规则,循环获取网址是否存在

    php循环规则,循环获取网址是否存在

  • 一机一码的加密视频,制作思路是怎样的

    一机一码的加密视频,制作思路是怎样的

  • 谁能不能告诉我这个网址是怎么做出来的

    谁能不能告诉我这个网址是怎么做出来的

腾讯云代理商
海外云服务器