前言:
此刻咱们对“python日历选择器选择日期”大约比较关怀,你们都想要分析一些“python日历选择器选择日期”的相关文章。那么小编也在网摘上搜集了一些有关“python日历选择器选择日期””的相关内容,希望咱们能喜欢,朋友们一起来学习一下吧!文章目录
本次使用PyQt5,打造一款时间管理工具,支持两种时间格式的显示、全屏、窗口的切换以及一些个性化的设置,可以用作计时器或者屏幕保护。
一.准备工作1.PyQt5
pyqt5是一套Python绑定Digia QT5应用的框架。它可用于Python 2和3。本教程使用Python 3。Qt库是最强大的GUI库之一。pyqt5的官方网站 。
pyqt5作为Python的一个模块,它有620多个类和6000个函数和方法。这是一个跨平台的工具包,它可以运行在所有主要的操作系统,包括UNIX,Windows,Mac OS。pyqt5是双重许可。开发者可以在GPL和商业许可之间进行选择。
2.Qt Designer
Qt设计师(Qt Designer)是使用Qt部件(Widgets)设计和使用图形用户界面(GUI)的工具。它允许我们以所见即所得的方式构建和定制自己的窗口(Windows)或对话框(Dialogs)并提供了不同的方法来测试它们。
二.预览
启动软件后,就会进入软件设置界面,通过选择时间显示格式、显示模式、颜色其他设置,可以预览要展示的时间。
点击开始后,就会进入软件主界面,软件会根据用户选择的模式和设置展示当前时间。
3.自定义样式
可以自定义日期时间显示样式
三.设计流程1.UI设计
1.my_time.ui
此为启动界面,整体垂直布局,局部水平布局。
2.my_timer.ui
此为主界面,整体水平布局。
四.源代码1.time_manger.py(主程序调用)
import sysfrom PyQt5.QtCore import *from PyQt5.QtWidgets import *from my_time import Ui_Set_Windowfrom my_timer import Ui_MainWindowclass My_Time(QDialog): def __init__(self): super().__init__() self.ui=Ui_Set_Window() self.ui.setupUi(self) self.main_ui=mainwindow() self.background_color_rgb_str="rgb(255, 255, 255)"#lcd背景颜色初始化 self.font_color_rgb_str="rgb(0, 0, 0)"#lcd字体颜色初始化 self.dateTimeFormat = "yyyy-MM-dd HH:mm:ss"#日期格式初始化 self.lcd_number = 19#lcd位数初始化 self.full_size_flag=1 self.ui.radioButton.toggled.connect(self.process_radiobtn) self.ui.radioButton_2.toggled.connect(self.process_radiobtn2) self.ui.radioButton_5.toggled.connect(self.process_radiobtn5) self.ui.radioButton_6.toggled.connect(self.process_radiobtn6) self.ui.pushButton_2.clicked.connect(self.select_background_color) self.ui.pushButton.clicked.connect(self.select_font_color) self.ui.pushButton_4.clicked.connect(self.start_timer) current_time = QDateTime.currentDateTime().toString(self.dateTimeFormat) self.ui.lcdNumber.display(current_time) qss_str = """ border: 2px solid black; color: {background_color} ; background:{font_color};""".replace( "{font_color}", self.font_color_rgb_str).replace(" {background_color}", self.background_color_rgb_str) self.ui.lcdNumber.setStyleSheet(qss_str) def process_radiobtn5(self): if self.ui.radioButton_5.isChecked(): self.main_ui.full_size_flag = True else: self.main_ui.full_size_flag = False def process_radiobtn6(self): if self.ui.radioButton_6.isChecked(): self.main_ui.full_size_flag=False else: self.main_ui.full_size_flag=True def process_radiobtn(self): if self.ui.radioButton.isChecked(): self.dateTimeFormat = "yyyy-MM-dd HH:mm:ss" self.lcd_number=19 current_time = QDateTime.currentDateTime().toString(self.dateTimeFormat) self.ui.lcdNumber.display(current_time) def process_radiobtn2(self): if self.ui.radioButton_2.isChecked(): self.dateTimeFormat="HH:mm:ss" self.lcd_number=8 current_time = QDateTime.currentDateTime().toString(self.dateTimeFormat) self.ui.lcdNumber.display(current_time) def select_background_color(self): color=QColorDialog.getColor() if color.isValid(): self.background_color_rgb_str=f"rgb({ color.red()},{ color.green()},{ color.blue()})" qss_str = """ border: 2px solid black; color: {background_color} ; background:{font_color};""".replace( "{font_color}", self.font_color_rgb_str).replace(" {background_color}", self.background_color_rgb_str) self.ui.lcdNumber.setStyleSheet(qss_str) def select_font_color(self): color=QColorDialog.getColor() if color.isValid(): self.font_color_rgb_str=f"rgb({ color.red()},{ color.green()},{ color.blue()})" qss_str = """ border: 2px solid black; color: {background_color} ; background:{font_color};""".replace( "{font_color}", self.font_color_rgb_str).replace(" {background_color}", self.background_color_rgb_str) self.ui.lcdNumber.setStyleSheet(qss_str) def start_timer(self,): #窗口关闭,发射信号,启动主窗口 self.main_ui.set_lcd_number.emit(self.lcd_number) self.main_ui.time_format.emit(self.dateTimeFormat) qss_str=""" border: 2px solid black; color: {background_color} ; background:{font_color};""".replace("{font_color}",self.font_color_rgb_str).replace(" {background_color}",self.background_color_rgb_str) self.main_ui.set_qss_single.emit(qss_str) if self.ui.checkBox_2.isChecked(): self.main_ui.show_time_on_title_flag=True else: self.main_ui.show_time_on_title_flag=False self.hide() self.main_ui.show() self.main_ui.start() def closeEvent(self,event): reply = QMessageBox.question(self, '关闭', "确定要退出吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if reply == QMessageBox.Yes: event.accept() else: event.ignore()class mainwindow(QMainWindow): set_qss_single=pyqtSignal(str) set_lcd_number=pyqtSignal(int) time_format=pyqtSignal(str) def __init__(self): super().__init__() self.ui=Ui_MainWindow() self.ui.setupUi(self) self.set_qss_single.connect(self.set_lcd_qss) self.time_format.connect(self.set_time_format) self.set_lcd_number.connect(self.set_lcd_number_) self.show_time_on_title_flag=True self.full_size_flag=True def do_procress_full_size(self,flag): if flag==True: self.showFullScreen()#全屏幕 elif flag==False: self.showNormal()#正常窗口化 def start(self): self.timer = QTimer(self) self.timer.setInterval(1000) self.do_procress_full_size(self.full_size_flag) self.timer.timeout.connect(self.show_current_time) self.timer.start() @pyqtSlot(str) def set_lcd_qss(self,qss): self.ui.lcdNumber.setStyleSheet(qss) @pyqtSlot(int) def set_lcd_number_(self,number): self.lcd_number=number self.ui.lcdNumber.setDigitCount(number) @pyqtSlot(str) def set_time_format(self,time_fmt): self.time_fmt=time_fmt def show_current_time(self): if self.lcd_number==19: current_time=QDateTime.currentDateTime().toString(self.time_fmt) else: current_time=QTime.currentTime().toString(self.time_fmt) if self.show_time_on_title_flag: self.setWindowTitle(current_time) else: self.setWindowTitle("时间管理") self.ui.lcdNumber.display(current_time) def closeEvent(self,event): reply = QMessageBox.question(self, '关闭', "确定要退出吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if reply == QMessageBox.Yes: event.accept() ui.show() self.timer.stop() else: event.ignore() def keyPressEvent(self, event): if event.key() == Qt.Key_Escape: # 当我们按住键盘是esc按键时 self.close() # 关闭程序 ui.show()if __name__ == '__main__': app=QApplication(sys.argv) ui=My_Time() ui.show() sys.exit(app.exec_())2.my_time.py(设置界面)
# -*- coding: utf-8 -*-from PyQt5 import QtCore, QtWidgetsclass Ui_Set_Window(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.setEnabled(True) Dialog.resize(461, 410) self.verticalLayout_2 = QtWidgets.QVBoxLayout(Dialog) self.verticalLayout_2.setObjectName("verticalLayout_2") self.groupBox = QtWidgets.QGroupBox(Dialog) self.groupBox.setObjectName("groupBox") self.verticalLayout = QtWidgets.QVBoxLayout(self.groupBox) self.verticalLayout.setObjectName("verticalLayout") self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.radioButton = QtWidgets.QRadioButton(self.groupBox) self.radioButton.setChecked(True) self.radioButton.setObjectName("radioButton") self.horizontalLayout.addWidget(self.radioButton) self.radioButton_2 = QtWidgets.QRadioButton(self.groupBox) self.radioButton_2.setObjectName("radioButton_2") self.horizontalLayout.addWidget(self.radioButton_2) self.horizontalLayout.setStretch(0, 2) self.horizontalLayout.setStretch(1, 1) self.verticalLayout.addLayout(self.horizontalLayout) self.verticalLayout_2.addWidget(self.groupBox) self.groupBox_4 = QtWidgets.QGroupBox(Dialog) self.groupBox_4.setObjectName("groupBox_4") self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.groupBox_4) self.verticalLayout_3.setObjectName("verticalLayout_3") self.horizontalLayout_7 = QtWidgets.QHBoxLayout() self.horizontalLayout_7.setObjectName("horizontalLayout_7") self.radioButton_5 = QtWidgets.QRadioButton(self.groupBox_4) self.radioButton_5.setChecked(True) self.radioButton_5.setObjectName("radioButton_5") self.horizontalLayout_7.addWidget(self.radioButton_5) self.radioButton_6 = QtWidgets.QRadioButton(self.groupBox_4) self.radioButton_6.setObjectName("radioButton_6") self.horizontalLayout_7.addWidget(self.radioButton_6) self.horizontalLayout_7.setStretch(0, 2) self.horizontalLayout_7.setStretch(1, 1) self.verticalLayout_3.addLayout(self.horizontalLayout_7) self.verticalLayout_2.addWidget(self.groupBox_4) self.groupBox_3 = QtWidgets.QGroupBox(Dialog) self.groupBox_3.setObjectName("groupBox_3") self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.groupBox_3) self.horizontalLayout_4.setObjectName("horizontalLayout_4") self.horizontalLayout_3 = QtWidgets.QHBoxLayout() self.horizontalLayout_3.setObjectName("horizontalLayout_3") self.pushButton_2 = QtWidgets.QPushButton(self.groupBox_3) self.pushButton_2.setMinimumSize(QtCore.QSize(100, 0)) self.pushButton_2.setObjectName("pushButton_2") self.horizontalLayout_3.addWidget(self.pushButton_2) spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_3.addItem(spacerItem) self.pushButton = QtWidgets.QPushButton(self.groupBox_3) self.pushButton.setMinimumSize(QtCore.QSize(100, 0)) self.pushButton.setObjectName("pushButton") self.horizontalLayout_3.addWidget(self.pushButton) spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_3.addItem(spacerItem1) self.checkBox_2 = QtWidgets.QCheckBox(self.groupBox_3) self.checkBox_2.setChecked(True) self.checkBox_2.setObjectName("checkBox_2") self.horizontalLayout_3.addWidget(self.checkBox_2) self.horizontalLayout_4.addLayout(self.horizontalLayout_3) self.verticalLayout_2.addWidget(self.groupBox_3) self.groupBox_2 = QtWidgets.QGroupBox(Dialog) self.groupBox_2.setObjectName("groupBox_2") self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.groupBox_2) self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.lcdNumber = QtWidgets.QLCDNumber(self.groupBox_2) self.lcdNumber.setLayoutDirection(QtCore.Qt.LeftToRight) self.lcdNumber.setFrameShape(QtWidgets.QFrame.NoFrame) self.lcdNumber.setSmallDecimalPoint(True) self.lcdNumber.setDigitCount(19) self.lcdNumber.setSegmentStyle(QtWidgets.QLCDNumber.Filled) self.lcdNumber.setProperty("value", 0.0) self.lcdNumber.setProperty("intValue", 0) self.lcdNumber.setObjectName("lcdNumber") self.horizontalLayout_2.addWidget(self.lcdNumber) self.horizontalLayout_2.setStretch(0, 10) self.verticalLayout_2.addWidget(self.groupBox_2) self.frame = QtWidgets.QFrame(Dialog) self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel) self.frame.setFrameShadow(QtWidgets.QFrame.Raised) self.frame.setObjectName("frame") self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.frame) self.horizontalLayout_5.setObjectName("horizontalLayout_5") self.horizontalLayout_6 = QtWidgets.QHBoxLayout() self.horizontalLayout_6.setObjectName("horizontalLayout_6") self.pushButton_4 = QtWidgets.QPushButton(self.frame) self.pushButton_4.setObjectName("pushButton_4") self.horizontalLayout_6.addWidget(self.pushButton_4) self.pushButton_5 = QtWidgets.QPushButton(self.frame) self.pushButton_5.setObjectName("pushButton_5") self.horizontalLayout_6.addWidget(self.pushButton_5) self.horizontalLayout_5.addLayout(self.horizontalLayout_6) self.verticalLayout_2.addWidget(self.frame) self.retranslateUi(Dialog) self.pushButton_5.clicked.connect(Dialog.close) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "时间管理")) self.groupBox.setTitle(_translate("Dialog", "时间显示格式")) self.radioButton.setText(_translate("Dialog", "年:月:日 时:分:秒")) self.radioButton_2.setText(_translate("Dialog", "时:分:秒")) self.groupBox_4.setTitle(_translate("Dialog", "模式选择")) self.radioButton_5.setText(_translate("Dialog", "全屏")) self.radioButton_6.setText(_translate("Dialog", "窗口")) self.groupBox_3.setTitle(_translate("Dialog", "颜色、其他设置")) self.pushButton_2.setText(_translate("Dialog", "字体色")) self.pushButton.setText(_translate("Dialog", "背景色")) self.checkBox_2.setText(_translate("Dialog", "窗口标题为当前时间")) self.groupBox_2.setTitle(_translate("Dialog", "预览")) self.pushButton_4.setText(_translate("Dialog", "开始")) self.pushButton_5.setText(_translate("Dialog", "退出"))3.my_timer.py(主界面)
# -*- coding: utf-8 -*-from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(574, 329) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setObjectName("verticalLayout") self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget) self.lcdNumber.setSmallDecimalPoint(True) self.lcdNumber.setDigitCount(8) self.lcdNumber.setMode(QtWidgets.QLCDNumber.Dec) self.lcdNumber.setProperty("value", 0.0) self.lcdNumber.setObjectName("lcdNumber") self.verticalLayout.addWidget(self.lcdNumber) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "时间管理"))
原文 链接
标签: #python日历选择器选择日期