Write a very simple graphical Python alarm using QT

  • 2020-05-05 11:29:07
  • OfStack

Today we're going to talk about the GUI applet written in Python. A small alarm clock (just a screen prompt, no sound)

Let's first introduce how weird this alarm clock is.

It needs to be started from the command line.

No title bar.

No menu.

There's not even a close button.

There is no running interface.

See here we must ask, why to do a program so weak. Obviously, the significance of education is greater than its practical use.

Like other modules, the QT bound bread needs to be loaded.

We used command line input, so the sys module is also required.

Time is up, but we don't need to use the time module, we use the QTime module provided by QT.

Let's start with the import statement section.
 


import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
app = QApplication(sys.argv)

This is a class that controls the lifeblood of the qt program, as we'll explain later. In every Qt program, you will find a similar statement.

Then it's time to enter the alarm clock. On the program.
 


try:
  message = "Alert!"
  if len(sys.argv) < 2:
    raise ValueError
  hours, mins = sys.argv[1].split(":")
  due = QTime(int(hours), int(mins))
  if not due.isValid():
    raise ValueError
  if len(sys.argv) > 2:
    message = " ".join(sys.argv[2:])
except ValueError:
  message = "Usage: alert.pyw HH:MM [optional message]"

Exception handling is included, and for an application, we should take all exception cases into account.

The program USES raise to trigger exceptions. Force the program into our intended orbit.

Obviously, the above program is not enough, it does not have time to judge and GUI part.

So how do we know if the time is up? Well, we give you a less accurate method, which is timing.
 


import time
while QTime.currentTime() < due:
# dormancy 20 seconds 
  time.sleep(20)

When the time is up we will jump to the next step, create a form, delay 60s and close.
 


label = QLabel("<font color=red size=72><b>" + message + "</b></font>")
label.setWindowFlags(Qt.SplashScreen)
label.show()
QTimer.singleShot(60000, app.quit) # 1 minute
app.exec_()

It seems that we need to write an introduction to the QTime module again.

Now put all the sentences together. Let's test it out.


Complete code:
 


import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
app = QApplication(sys.argv)
try:
  message = "Alert!"
  if len(sys.argv) < 2:
    raise ValueError
  hours, mins = sys.argv[1].split(":")
  due = QTime(int(hours), int(mins))
  if not due.isValid():
    raise ValueError
  if len(sys.argv) > 2:
    message = " ".join(sys.argv[2:])
except ValueError:
  message = "Usage: alert.pyw HH:MM [optional message]" # 24hr clock
while QTime.currentTime() < due:
  time.sleep(20) # 20 seconds
label = QLabel("<font color=red size=72><b>" + message + "</b></font>")
label.setWindowFlags(Qt.SplashScreen)
label.show()
QTimer.singleShot(60000, app.quit) # 1 minute
app.exec_()


Related articles: