Skip to content

Instantly share code, notes, and snippets.

@santosh
Last active July 30, 2018 11:40
Show Gist options
  • Save santosh/b80248cabecbffbcc43397d07ac6b71f to your computer and use it in GitHub Desktop.
Save santosh/b80248cabecbffbcc43397d07ac6b71f to your computer and use it in GitHub Desktop.
Calculate hours passed since your birth. #Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
File: ageinhours.py
Author: Santosh Kumar
Email: [email protected]
Github: @santosh
Description: Calculate age in seconds
"""
import sys
import datetime
try:
from PySide import QtGui, QtCore
except ModuleNotFoundError:
from PySide2.QtWidgets import QtGui
from PySide2 import QtCore
class AgeInHours(QtGui.QMainWindow):
"""The main window of AgeInHours"""
def __init__(self):
super(self.__class__, self).__init__()
self.setWindowTitle("Age in Hours")
self.setGeometry(200, 200, 350, 150)
# self.setWindowIcon(QtGui.QIcon("favicon.png"))
self.initUi()
def initUi(self):
self.centralWidget = QtGui.QWidget(self)
lyt_central = QtGui.QVBoxLayout(self.centralWidget)
lbl_instructions = QtGui.QLabel("How much time has passed \
since your birth?")
lyt_year = QtGui.QHBoxLayout()
lbl_year = QtGui.QLabel("Year: ")
self.slr_year = QtGui.QSlider(QtCore.Qt.Horizontal)
self.slr_year.setMinimum(1980)
self.slr_year.setMaximum(2018)
self.slr_year.setValue(1996)
self.slr_year.setTickPosition(QtGui.QSlider.TicksBelow)
self.slr_year.setTickInterval(1)
self.slr_year.valueChanged.connect(self.year_change)
self.slr_year.valueChanged.connect(self.calculate_hours)
self.led_year = QtGui.QLineEdit()
self.led_year.returnPressed.connect(
lambda: self.slr_year.setValue(int(self.led_year.text())))
self.led_year.setFixedWidth(55)
lyt_year.addWidget(lbl_year)
lyt_year.addWidget(self.slr_year)
lyt_year.addWidget(self.led_year)
lyt_month = QtGui.QHBoxLayout()
lbl_month = QtGui.QLabel("Month: ")
self.slr_month = QtGui.QSlider(QtCore.Qt.Horizontal)
self.slr_month.setMinimum(1)
self.slr_month.setMaximum(12)
self.slr_month.setValue(10)
self.slr_month.setTickPosition(QtGui.QSlider.TicksBelow)
self.slr_month.setTickInterval(1)
self.slr_month.valueChanged.connect(self.month_change)
self.slr_month.valueChanged.connect(self.calculate_hours)
self.led_month = QtGui.QLineEdit()
self.led_month.returnPressed.connect(
lambda: self.slr_month.setValue(int(self.led_month.text())))
self.led_month.setFixedWidth(55)
lyt_month.addWidget(lbl_month)
lyt_month.addWidget(self.slr_month)
lyt_month.addWidget(self.led_month)
lyt_day = QtGui.QHBoxLayout()
lbl_day = QtGui.QLabel("Day: ") # extra spaces to match alignment
self.slr_day = QtGui.QSlider(QtCore.Qt.Horizontal)
self.slr_day.setMinimum(1)
self.slr_day.setMaximum(31)
self.slr_day.setValue(12)
self.slr_day.setTickPosition(QtGui.QSlider.TicksBelow)
self.slr_day.setTickInterval(1)
self.slr_day.valueChanged.connect(self.day_change)
self.slr_day.valueChanged.connect(self.calculate_hours)
self.led_day = QtGui.QLineEdit()
self.led_day.returnPressed.connect(
lambda: self.slr_day.setValue(int(self.led_day.text())))
self.led_day.setFixedWidth(55)
lyt_day.addWidget(lbl_day)
lyt_day.addWidget(self.slr_day)
lyt_day.addWidget(self.led_day)
lyt_output = QtGui.QHBoxLayout()
lbl_output = QtGui.QLabel("Your age in hours is approximately: ")
self.lbl_output_data = QtGui.QLabel()
lyt_output.addWidget(lbl_output)
lyt_output.addWidget(self.lbl_output_data)
lyt_central.addWidget(lbl_instructions)
lyt_central.addLayout(lyt_year)
lyt_central.addLayout(lyt_month)
lyt_central.addLayout(lyt_day)
lyt_central.addLayout(lyt_output)
self.setCentralWidget(self.centralWidget)
def year_change(self, value):
self.led_year.setText(str(value))
def month_change(self, value):
self.led_month.setText(str(value))
def day_change(self, value):
self.led_day.setText(str(value))
def calculate_hours(self):
"""Get years, months and days and update the age."""
year = int(self.slr_year.value())
month = int(self.slr_month.value())
day = int(self.slr_day.value())
current_time = datetime.datetime.now()
birth_time = datetime.datetime(year, month, day)
time_diff = current_time - birth_time
hours = round(time_diff.total_seconds() / 3600)
self.lbl_output_data.setText(str(hours))
def keyPressEvent(self, e):
"""Response to keypresses."""
if e.key() == QtCore.Qt.Key_Escape:
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = AgeInHours()
w.show()
sys.exit(app.exec_())
@santosh
Copy link
Author

santosh commented Jul 17, 2018

See this post and change the code accordingly.

@santosh
Copy link
Author

santosh commented Jul 30, 2018

Updated the code with datetime module integration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment