一、运行环境
1、python 3.7(个人感觉不是第2代就行)
2、tk pythom已经内置
3、pycharm 2020
4、zhdate 0.1(阴历转换包)
二、无图无真相
编辑
三、代码
使用after更新时间
def update_clock(self):
#now = time.strftime("%H:%M:%S")
now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
#self.lable_week.configure(text=now_time)
self.lable_week['text'] = '今天是:{0}'.format(DateTools().week_china_day())
self.lable_time['text'] = '当前时间:{0}'.format(now_time)
self.frame.after(1000, self.update_clock)
线程更新时间
def show_time(self):
self.T = threading.Thread(target=self.__show_time, args=())
self.T.setDaemon(True)
self.T.start()
def __show_time(self):
while 1:
now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
self.lable_week['text'] = '今天是:{0}'.format(DateTools().week_china_day())
self.lable_time['text'] = '当前时间:{0}'.format(now_time)
time.sleep(1)
当前日期转阴历
from zhdate import ZhDate
from datetime import datetime
year = int(time.strftime('%Y', time.localtime(time.time())))
month = int(time.strftime('%m', time.localtime(time.time())))
day = int(time.strftime('%d', time.localtime(time.time())))
d2 = datetime(year, month, day)
date2 = ZhDate.from_datetime(d2)
self.lable_zhdate['text'] = '{0}'.format(date2)
完整代码
import datetime
from tkinter import ttk
import tkinter
import tkinter as tk # 使用Tkinter前需要先导入
from framework.base.BaseFrame import BaseFrame
from tkinter import *
import threading
import time
from framework.control.AdenButton import AdenButton
from framework.control.AdenLabel import AdenLabel
from framework.control.AdenTkPlugin import TkPlugin
from framework.pulgin.DateTools import DateTools
from framework.pulgin.Tools import Tools
class ClockTimeView(BaseFrame):
def __init__(self):
# do this before importing pylab or pyplot
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
pass
def getFrame(self, root, notebook):
def computer_time(event):
# hour = self.com_hour.get()
# minutes = self.com_minutes.get()
#
# current_date_and_time =datetime.datetime.now()# Tools.get_current_datetime()
# new_time = current_date_and_time datetime.timedelta(minutes=int(minutes), hours=int(hour))
# # print(type(new_time))
# # print(type(local_time)) datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# self.new_time.set (str(new_time.strftime("%Y-%m-%d %H:%M:%S")))
pass
self.note_book = notebook
self.frame = tkinter.Frame(root)
self.frame.configure(background=TkPlugin.background())
self.frame.rowconfigure(0, weight=1) # 设置第2列自动适应Frame 需要sitcky布局配合
self.frame.rowconfigure(5, weight=1)
self.frame.columnconfigure(0, weight=1) # 设置第2列自动适应Frame 需要sitcky布局配合
self.frame.columnconfigure(2, weight=1)
# 第1行
self.lable_time1 = AdenLabel(self.frame, text="", font='Helvetica -30 bold')
self.lable_time1.grid(row=0, column=0,columnspan=3, sticky=TkPlugin.sticky_all())
# 第2行
self.lable_week = AdenLabel(self.frame, text="今天是星期三", font = 'Helvetica -30 bold')
self.lable_week.grid(row=1, column=0,columnspan=3, sticky=TkPlugin.sticky_all())
# 第3行
self.lable_time = AdenLabel(self.frame, text="当前时间:2021-11-11 9:37:20", font = 'Helvetica -30 bold')
self.lable_time.grid(row=2, column=0,columnspan=3, sticky=TkPlugin.sticky_all())
# 第4行 sticky=W E N S
self.lable_zhdate = AdenLabel(self.frame, text="增加时间:", font='Helvetica -30 bold')
self.lable_zhdate.grid(row=3, column=0, sticky=E)
from zhdate import ZhDate
from datetime import datetime
year = int(time.strftime('%Y', time.localtime(time.time())))
month = int(time.strftime('%m', time.localtime(time.time())))
day = int(time.strftime('%d', time.localtime(time.time())))
d2 = datetime(year, month, day)
date2 = ZhDate.from_datetime(d2)
self.lable_zhdate['text'] = '{0}'.format(date2)
# 第5行
self.new_time = tk.StringVar()
self.lable_time5 = AdenLabel(self.frame, textvariable=self.new_time, font='Helvetica -30 bold')
self.lable_time5.grid(row=4, column=0, columnspan=3, sticky=TkPlugin.sticky_all())
# 第6行
self.lable_time6 = AdenLabel(self.frame, text="", font='Helvetica -30 bold')
self.lable_time6.grid(row=5, column=0, columnspan=3, sticky=TkPlugin.sticky_all())
#第七行
btnSave = AdenButton(self.frame, text="清空", width=8, command=lambda: self.void())
btnSave.grid(row=6, column=0, sticky=tkinter.E, columnspan="3", padx="70", )
btnDelete = AdenButton(self.frame, text="关闭窗体", bg="#D9534F", width=8, command=lambda: self.forget())
btnDelete.grid(row=6, column=0, sticky=tkinter.E, columnspan="3")
self.update_clock()
return self.frame
def forget(self):
# if self.T is not None:
# Tools.stop_thread(self.T)
self.note_book.forget(self.frame)
pass
def void(self):
self.new_time.set("")
pass
def update_clock(self):
#now = time.strftime("%H:%M:%S")
now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
#self.lable_week.configure(text=now_time)
self.lable_week['text'] = '今天是:{0}'.format(DateTools().week_china_day())
self.lable_time['text'] = '当前时间:{0}'.format(now_time)
self.frame.after(1000, self.update_clock)
def show_time(self):
self.T = threading.Thread(target=self.__show_time, args=())
self.T.setDaemon(True)
self.T.start()
def __show_time(self):
while 1:
now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
self.lable_week['text'] = '今天是:{0}'.format(DateTools().week_china_day())
self.lable_time['text'] = '当前时间:{0}'.format(now_time)
time.sleep(1)