PyQt5 – 日期的天数计算器

PyQt5 – 日期的天数计算器

在这篇文章中,我们将看到如何在PyQt5中创建一个从日期算起的天数计算器,从日期算起的天数计算器是用来从选定的日期中添加或减去天数,从而得到新的日期。这个计算器是用来得到给定天数后的确切日期的。下面是这个计算器的样子

PyQt5 - 日期的天数计算器

PyQt5 是跨平台的GUI工具包,是Qt v5的一组python绑定。 由于这个库提供的工具和简单性,人们可以非常容易地开发一个交互式的桌面应用程序。以下是安装PyQt5的命令

pip install PyQt5

GUI实现步骤:

1.创建一个标题标签,显示计算器名称

2.创建标签以显示用户选择日期

3.创建一个QCalendarWidget对象让用户选择日期

4.创建一个标签和旋转框,告诉用户输入天数并获得天数

5.创建两个按钮用于添加和减去天数

6.创建一个标签以显示计算出的日期

后台实现:

1.为这两个按钮添加动作

2.在加法按钮的动作中,获取天数并调用计算方法,并将天数作为参数传给

3.在减去按钮的动作中,获得天数,使天数为负数,调用计算方法,并将天数作为参数传给

4.在计算方法中获得日历中的选定日期

5.将通过的天数加入到选定的日期中

6.在结果标签的帮助下,显示新的日期。

以下是实现方法

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import datetime
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # width of window
        self.w_width = 400
  
        # height of window
        self.w_height = 530
  
        # setting geometry
        self.setGeometry(100, 100, self.w_width, self.w_height)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # method for components
    def UiComponents(self):
        # creating head label
        head = QLabel("+/- Days from a date Calculator", self)
  
        head.setWordWrap(True)
  
        # setting geometry to the head
        head.setGeometry(0, 10, 400, 60)
  
        # font
        font = QFont('Times', 15)
        font.setBold(True)
        font.setItalic(True)
        font.setUnderline(True)
  
        # setting font to the head
        head.setFont(font)
  
        # setting alignment of the head
        head.setAlignment(Qt.AlignCenter)
  
        # setting color effect to the head
        color = QGraphicsColorizeEffect(self)
        color.setColor(Qt.darkCyan)
        head.setGraphicsEffect(color)
  
        # creating a label
        b_label = QLabel("Select Date", self)
  
        # setting properties  label
        b_label.setAlignment(Qt.AlignCenter)
        b_label.setGeometry(50, 100, 300, 20)
        b_label.setStyleSheet("QLabel"
                              "{"
                              "border : 1px solid black;"
                              "background : rgba(70, 70, 70, 25);"
                              "}")
        b_label.setFont(QFont('Times', 9))
  
        # creating a calendar widget to select the date
        self.calendar = QCalendarWidget(self)
  
        # setting geometry of the calendar
        self.calendar.setGeometry(50, 120, 300, 180)
  
        # setting font to the calendar
        self.calendar.setFont(QFont('Times', 6))
  
  
        # creating a label
        days_label = QLabel("Days", self)
  
        # setting geometry to the label
        days_label.setGeometry(50, 320, 147, 40)
  
        # setting alignment
        days_label.setAlignment(Qt.AlignCenter)
  
        # setting stylesheet
        days_label.setStyleSheet("QLabel"
                                 "{"
                                 "border : 2px solid black;"
                                 "background : rgba(70, 70, 70, 35);"
                                 "}")
  
        days_label.setFont(QFont('Times', 9))
  
        # creating a spin box to get the days
        self.days = QSpinBox(self)
  
        # setting geometry to the spin box
        self.days.setGeometry(203, 320, 147, 40)
  
        # setting maximum value of spin box
        self.days.setMaximum(99999999)
  
        # setting font and alignment
        self.days.setFont(QFont('Times', 9))
        self.days.setAlignment(Qt.AlignCenter)
  
  
  
        # creating a push button
        add = QPushButton("Add Days", self)
  
        # setting geometry to the push button
        add.setGeometry(80, 380, 100, 40)
  
        # adding action to the button
        add.clicked.connect(self.add_action)
  
        # adding color effect to the push button
        color = QGraphicsColorizeEffect()
        color.setColor(Qt.blue)
        add.setGraphicsEffect(color)
  
        # creating a push button
        subtract = QPushButton("Subtract Days", self)
  
        # setting geometry to the push button
        subtract.setGeometry(220, 380, 100, 40)
  
        # adding action to the  button
        subtract.clicked.connect(self.subtract_action)
  
        # adding color effect to the push button
        color = QGraphicsColorizeEffect()
        color.setColor(Qt.red)
        subtract.setGraphicsEffect(color)
  
  
        # creating a label to show result
        self.result = QLabel(self)
  
        # setting properties to result label
        self.result.setAlignment(Qt.AlignCenter)
  
        # setting geometry
        self.result.setGeometry(50, 440, 300, 60)
  
        # making it multi line
        self.result.setWordWrap(True)
  
        # setting stylesheet
        # adding border and background
        self.result.setStyleSheet("QLabel"
                                  "{"
                                  "border : 3px solid black;"
                                  "background : white;"
                                  "}")
  
        # setting font
        self.result.setFont(QFont('Arial', 11))
  
    # method called by the add push button
    def add_action(self):
  
        # get the days from the spin box
        days = self.days.value()
  
        # call the calculate action
        self.calculate(days)
  
  
    # method called by the subtract push button
    def subtract_action(self):
  
        # get the days from the spin box
        # make the days value negative
        days = 0 - self.days.value()
  
        # call the calculate action
        self.calculate(days)
  
  
  
    def calculate(self, days):
  
        # get the selected date of calendar
        selected_date = self.calendar.selectedDate()
  
        # adding days to the selected days
        new_date = selected_date.addDays(days)
  
        # showing this date through label
        self.result.setText("Date : " + new_date.toString(Qt.ISODate))
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程