Created
October 10, 2019 13:50
-
-
Save re4388/35f738708ee3264a6b07f837423f649b to your computer and use it in GitHub Desktop.
My First PyQt5 app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5.QtWidgets import QApplication, QWidget,QLabel, QMessageBox, QPushButton | |
from PyQt5.QtGui import QIcon, QPixmap | |
from PyQt5.QtCore import pyqtSlot, QTimer, QDateTime, QDate, QTime | |
from Ui_Pomodoro import Ui_Dialog | |
from PyQt5 import QtCore | |
import time | |
class MyTimer(QWidget): | |
def __init__(self): | |
super().__init__() | |
self.setGeometry(600,300,380,290) | |
self.setFixedSize(self.size()) | |
# show image as bg | |
label = QLabel(self) | |
pixmap = QPixmap('promodoro.png') | |
# pixmap = QPixmap('a3.jpg') | |
label.setPixmap(pixmap) | |
self.resize(pixmap.width(), pixmap.height()) | |
# call designer UI | |
self.ui = Ui_Dialog() | |
self.ui.setupUi(self) | |
# how many chr in put into lcd | |
self.ui.lcdNumber.setDigitCount(5) | |
# Create a timer | |
self.timer = QTimer() | |
# Connect the timer to the appropriate slot | |
# this need to be at this level! | |
self.timer.timeout.connect(self.count_down) | |
@pyqtSlot() | |
def on_pushButtonStop_clicked(self): | |
self.timer.stop() | |
self.min = 0 | |
self.sec = 0 | |
self.ui.lcdNumber.display(f"{str(self.min)}:{str(self.sec)}") | |
@pyqtSlot() | |
def on_pushButtonStart_clicked(self): | |
# get sec value | |
self.sec = self.ui.spinBoxSec.value() | |
self.min = self.ui.spinBoxMin.value() | |
self.ui.lcdNumber.display(f"{str(self.min)}:{str(self.sec)}") | |
# Wait one second between slot calls | |
self.timer.start(1000) | |
def count_down(self): | |
""" | |
it will count down the sec | |
it will decrease one min when sec hit 0 | |
it will stop when sec and min are both equal to 0 | |
""" | |
self.ui.lcdNumber.display(f"{str(self.min)}:{str(self.sec)}") | |
print(self.sec) | |
if self.sec == 0 and self.min > 0: | |
print('min minus one') | |
self.min -= 1 | |
self.sec = 59 | |
elif self.sec == 0 and self.min == 0: | |
print('stop timer') | |
self.timer.stop() | |
self.ui.spinBoxSec.setValue(0) | |
self.ui.spinBoxMin.setValue(0) | |
self.msg() | |
else: | |
self.sec = self.sec - 1 | |
def msg(self): | |
reply = QMessageBox.information(self, | |
"Time is Up", | |
"Have a break!!", | |
QMessageBox.Yes | QMessageBox.No) | |
if __name__ == "__main__": | |
app = QApplication(sys.argv) | |
win = MyTimer() | |
win.show() | |
sys.exit(app.exec_()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Form implementation generated from reading ui file 'd:\ben-code\python\pyqt5\project-promodoro\Pomodoro.ui' | |
# | |
# Created by: PyQt5 UI code generator 5.13.0 | |
# | |
# WARNING! All changes made in this file will be lost! | |
from PyQt5 import QtCore, QtGui, QtWidgets | |
class Ui_Dialog(object): | |
def setupUi(self, Dialog): | |
Dialog.setObjectName("Dialog") | |
Dialog.resize(382, 300) | |
font = QtGui.QFont() | |
font.setBold(False) | |
font.setWeight(50) | |
Dialog.setFont(font) | |
self.labelMin = QtWidgets.QLabel(Dialog) | |
self.labelMin.setGeometry(QtCore.QRect(20, 200, 31, 21)) | |
font = QtGui.QFont() | |
font.setPointSize(10) | |
font.setBold(False) | |
font.setWeight(50) | |
self.labelMin.setFont(font) | |
self.labelMin.setAutoFillBackground(True) | |
self.labelMin.setObjectName("labelMin") | |
self.lcdNumber = QtWidgets.QLCDNumber(Dialog) | |
self.lcdNumber.setGeometry(QtCore.QRect(10, 100, 91, 41)) | |
font = QtGui.QFont() | |
font.setPointSize(9) | |
font.setBold(False) | |
font.setWeight(50) | |
font.setStrikeOut(False) | |
font.setKerning(False) | |
self.lcdNumber.setFont(font) | |
self.lcdNumber.setMode(QtWidgets.QLCDNumber.Dec) | |
self.lcdNumber.setObjectName("lcdNumber") | |
self.pushButtonStart = QtWidgets.QPushButton(Dialog) | |
self.pushButtonStart.setGeometry(QtCore.QRect(90, 260, 93, 28)) | |
font = QtGui.QFont() | |
font.setPointSize(12) | |
font.setBold(False) | |
font.setWeight(50) | |
self.pushButtonStart.setFont(font) | |
self.pushButtonStart.setObjectName("pushButtonStart") | |
self.pushButtonStop = QtWidgets.QPushButton(Dialog) | |
self.pushButtonStop.setGeometry(QtCore.QRect(190, 260, 93, 28)) | |
font = QtGui.QFont() | |
font.setPointSize(12) | |
font.setBold(False) | |
font.setWeight(50) | |
self.pushButtonStop.setFont(font) | |
self.pushButtonStop.setObjectName("pushButtonStop") | |
self.spinBoxMin = QtWidgets.QSpinBox(Dialog) | |
self.spinBoxMin.setGeometry(QtCore.QRect(60, 200, 42, 22)) | |
self.spinBoxMin.setObjectName("spinBoxMin") | |
self.labelSec = QtWidgets.QLabel(Dialog) | |
self.labelSec.setGeometry(QtCore.QRect(20, 230, 31, 21)) | |
font = QtGui.QFont() | |
font.setPointSize(10) | |
font.setBold(False) | |
font.setWeight(50) | |
self.labelSec.setFont(font) | |
self.labelSec.setAutoFillBackground(True) | |
self.labelSec.setObjectName("labelSec") | |
self.spinBoxSec = QtWidgets.QSpinBox(Dialog) | |
self.spinBoxSec.setGeometry(QtCore.QRect(60, 230, 42, 22)) | |
self.spinBoxSec.setObjectName("spinBoxSec") | |
self.retranslateUi(Dialog) | |
QtCore.QMetaObject.connectSlotsByName(Dialog) | |
def retranslateUi(self, Dialog): | |
_translate = QtCore.QCoreApplication.translate | |
Dialog.setWindowTitle(_translate("Dialog", "Dialog")) | |
self.labelMin.setText(_translate("Dialog", "Mins")) | |
self.pushButtonStart.setText(_translate("Dialog", "Start")) | |
self.pushButtonStop.setText(_translate("Dialog", "Stop")) | |
self.labelSec.setText(_translate("Dialog", "Sec")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment