Python 小练习-11 电饭锅预约时间计算小程序

在这里插入图片描述

from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
    QInputDialog, QApplication, QLabel)
from PyQt5 import QtGui
import os
import sys
import re
import time
import datetime

class CostTimeCalculator(QWidget):

	def __init__(self):
		super().__init__()
		self.initTime()
		self.initUI()
		self.setWindowTitle('煮饭时间预约')
		self.setGeometry(100, 100, 480, 250)
		
	def initTime(self):
	
		self.initYear = datetime.datetime.now().year
		self.initMonth = datetime.datetime.now().month
		self.initDay = datetime.datetime.now().day
		self.initHour = datetime.datetime.now().hour
		self.initMin = datetime.datetime.now().minute
		self.initSec = datetime.datetime.now().second
	
	def initUI(self):
		self.label = QLabel("现在时间:",self)
		self.label.move(30,22)
		self.label.setFixedSize(60,20)
		
		self.label_year = QLabel("年",self)
		self.label_year.move(100,22)
		self.label_year.setFixedSize(20,20)
		
		self.le_year = QLineEdit(self)
		self.le_year.setReadOnly(False)        
		self.le_year.setFixedSize(30,20)
		self.le_year.setText(str(self.initYear))
		self.le_year.move(120, 22)  

		self.label_month = QLabel("月",self)
		self.label_month.move(160,22)
		self.label_month.setFixedSize(20,20) 
		
		self.le_month = QLineEdit(self)
		self.le_month.setReadOnly(False)        
		self.le_month.setFixedSize(30,20)
		self.le_month.setText(str(self.initMonth))
		self.le_month.move(180, 22) 
		
		self.label_day = QLabel("日",self)
		self.label_day.move(220,22)
		self.label_day.setFixedSize(20,20) 
		
		self.le_day = QLineEdit(self)
		self.le_day.setReadOnly(False)        
		self.le_day.setFixedSize(30,20)
		self.le_day.setText(str(self.initDay))
		self.le_day.move(240, 22) 
		
		self.label_hour = QLabel("时",self)
		self.label_hour.move(280,22)
		self.label_hour.setFixedSize(20,20) 
		
		self.le_hour = QLineEdit(self)
		self.le_hour.setReadOnly(False)        
		self.le_hour.setFixedSize(30,20)
		self.le_hour.setText(str(self.initHour))
		self.le_hour.move(300, 22) 
		
		self.label_min = QLabel("分",self)
		self.label_min.move(340,22)
		self.label_min.setFixedSize(20,20) 
		
		self.le_min = QLineEdit(self)
		self.le_min.setReadOnly(False)        
		self.le_min.setFixedSize(30,20)
		self.le_min.setText(str(self.initMin))
		self.le_min.move(360, 22) 
		
		self.label_sec = QLabel("秒",self)
		self.label_sec.move(400,22)
		self.label_sec.setFixedSize(20,20) 
		
		self.le_sec = QLineEdit(self)
		self.le_sec.setReadOnly(False)        
		self.le_sec.setFixedSize(30,20)
		self.le_sec.setText(str(self.initSec))
		self.le_sec.move(420, 22) 
		
		self.label_req = QLabel("预定时间:",self)
		self.label_req.move(30,50)
		self.label_req.setFixedSize(60,20)
		
		self.label_year_req = QLabel("年",self)
		self.label_year_req.move(100,50)
		self.label_year_req.setFixedSize(20,20)
		
		self.le_year_req = QLineEdit(self)
		self.le_year_req.setReadOnly(False)        
		self.le_year_req.setFixedSize(30,20)
		self.le_year_req.setText(str(self.initYear))
		self.le_year_req.move(120, 50)  

		self.label_month_req = QLabel("月",self)
		self.label_month_req.move(160,50)
		self.label_month_req.setFixedSize(20,20) 
		
		self.le_month_req = QLineEdit(self)
		self.le_month_req.setReadOnly(False)        
		self.le_month_req.setFixedSize(30,20)
		self.le_month_req.setText(str(self.initMonth))
		self.le_month_req.move(180, 50) 
		
		self.label_day_req = QLabel("日",self)
		self.label_day_req.move(220,50)
		self.label_day_req.setFixedSize(20,20) 
		
		self.le_day_req = QLineEdit(self)
		self.le_day_req.setReadOnly(False)        
		self.le_day_req.setFixedSize(30,20)
		self.le_day_req.setText(str(self.initDay))
		self.le_day_req.move(240, 50) 
		
		self.label_hour_req = QLabel("时",self)
		self.label_hour_req.move(280,50)
		self.label_hour_req.setFixedSize(20,20) 
		
		self.le_hour_req = QLineEdit(self)
		self.le_hour_req.setReadOnly(False)        
		self.le_hour_req.setFixedSize(30,20)
		self.le_hour_req.setText(str(self.initHour))
		self.le_hour_req.move(300, 50) 
		
		self.label_min_req = QLabel("分",self)
		self.label_min_req.move(340,50)
		self.label_min_req.setFixedSize(20,20) 
		
		self.le_min_req = QLineEdit(self)
		self.le_min_req.setReadOnly(False)        
		self.le_min_req.setFixedSize(30,20)
		self.le_min_req.setText(str(self.initMin))
		self.le_min_req.move(360, 50) 
		
		self.label_sec_req = QLabel("秒",self)
		self.label_sec_req.move(400,50)
		self.label_sec_req.setFixedSize(20,20) 
		
		self.le_sec_req = QLineEdit(self)
		self.le_sec_req.setReadOnly(False)        
		self.le_sec_req.setFixedSize(30,20)
		self.le_sec_req.setText(str(self.initSec))
		self.le_sec_req.move(420, 50) 		
		
		self.label_set = QLabel("需要设定",self)
		self.label_set.move(120,90)
		self.label_set.setFixedSize(60,20)
		
		self.le_set_hour = QLineEdit(self)
		self.le_set_hour.setReadOnly(False)        
		self.le_set_hour.setFixedSize(30,20)
		self.le_set_hour.setText("0")
		self.le_set_hour.move(180, 90)
		
		self.label_set_hour = QLabel("小时",self)
		self.label_set_hour.move(220,90)
		self.label_set_hour.setFixedSize(30,20)
		
		self.le_set_min = QLineEdit(self)
		self.le_set_min.setReadOnly(False)        
		self.le_set_min.setFixedSize(30,20)
		self.le_set_min.setText("0")
		self.le_set_min.move(250, 90)
		
		self.label_set = QLabel("分钟",self)
		self.label_set.move(290,90)
		self.label_set.setFixedSize(30,20)
		
		self.btncal_cal = QPushButton(self)
		self.btncal_cal.setText("计算")
		self.btncal_cal.clicked.connect(self.calculteFunction)
		self.btncal_cal.setFixedSize(40,22)
		self.btncal_cal.move(170, 130)
		
		self.btncal_reset = QPushButton(self)
		self.btncal_reset.setText("重置")
		self.btncal_reset.clicked.connect(self.reset)
		self.btncal_reset.setFixedSize(40,22)
		self.btncal_reset.move(250, 130)
		
		self.label_reminder = QLabel("提示:时间为24小时制",self)
		self.label_reminder.move(150,200)
		self.label_reminder.setFixedSize(150,20)
		
		self.label_warnning = QLabel("",self)
		self.label_warnning.move(150,230)
		self.label_warnning.setFixedSize(150,20)
		
	def calculteFunction(self):		
		
		# 将文本框中的字符串拼成"%Y-%m-%d %H:%M:%S"格式
		# 首先判断是否有输入框为空,如果有输入框为空则提示
		
		self.label_warnning.setText("")
		current_time_list = [self.le_year.text(),self.le_month.text(),self.le_day.text(),self.le_hour.text(),self.le_min.text(),self.le_sec.text()]
		expect_time_list = [self.le_year_req.text(),self.le_month_req.text(),self.le_day_req.text(),self.le_hour_req.text(),self.le_min_req.text(),self.le_sec_req.text()]
		
		for current_time_text in current_time_list:
			if len(current_time_text.strip()) == 0:
				self.label_warnning.setText("输入框不能为空!!!")
				return
				
		for expect_time_text in expect_time_list:
			if len(expect_time_text.strip()) == 0:
				self.label_warnning.setText("输入框不能为空!!!")
				return
			
		current_time = self.le_year.text()+"-"+self.le_month.text()+"-"+self.le_day.text()+" "+self.le_hour.text()+":"+self.le_min.text()+":"+self.le_sec.text()
		expect_time = self.le_year_req.text()+"-"+self.le_month_req.text()+"-"+self.le_day_req.text()+" "+self.le_hour_req.text()+":"+self.le_min_req.text()+":"+self.le_sec_req.text()
		
		print(current_time)
		print(expect_time)
		
		# 将字符串格式日期转为时间戳,防止输入的日期不存在使用trycatch来处理异常
		try:
			current_time_stamp = time.mktime(time.strptime(current_time,"%Y-%m-%d %H:%M:%S"))
			expect_time_stamp = time.mktime(time.strptime(expect_time,"%Y-%m-%d %H:%M:%S"))
		except Exception as ret:
			print(ret)
			self.label_warnning.setText("您的输入的时间不存在!!!")
			return
			
		cost_time = expect_time_stamp - current_time_stamp
		# 如果时间差不在(0,24)小时以内,则提示"您的输入超出范围!!!"
		if cost_time < 0 or cost_time > 86400:
			self.label_warnning.setText("您的输入超出范围!!!")
			return			

		# 计算秒位数值
		rest_seconds = cost_time%60
		# 计算时位数值
		rest_hour = (cost_time - rest_seconds)/3600
		# 计算分位数值
		rest_minute = ((cost_time - rest_seconds)%3600)/60
		
		self.le_set_min.setText(str(int(rest_minute)))
		self.le_set_hour.setText(str(int(rest_hour)))
				
		print(cost_time)
		print("%d小时%d分钟%d秒"%(rest_hour,rest_minute,rest_seconds))

		
	def reset(self):
	
		self.initTime()
		
		self.le_year.setText(str(self.initYear))
		self.le_month.setText(str(self.initMonth))
		self.le_day.setText(str(self.initDay))
		self.le_hour.setText(str(self.initHour))
		self.le_min.setText(str(self.initMin))		
		self.le_sec.setText(str(self.initSec))
		
		self.le_year_req.setText(str(self.initYear))
		self.le_month_req.setText(str(self.initMonth))
		self.le_day_req.setText(str(self.initDay))
		self.le_hour_req.setText(str(self.initHour))
		self.le_min_req.setText(str(self.initMin))
		self.le_sec_req.setText(str(self.initSec))
		
		self.le_sec_req.setText(str(self.initSec))
		print("clicked")

		
if __name__ == "__main__":
	app = QApplication(sys.argv)
	ctc = CostTimeCalculator()
	ctc.show()
	sys.exit(app.exec_())
	
	
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值