Today we'll talk about GUI applet written in Python. A small alarm clock (just a ScreenTip, no sound OH)
Let's first introduce how wonderful this alarm clock is.
Needs to be started from the command line.
There is no title bar.
No menu.
There is not even a close button.
There is no interface to run.
See here presumably everyone should ask, why to do a so weak explosion of the program it. Clearly, educational significance is greater than its practical usefulness.
Like other modules, qt bread needs to be loaded.
We used command line input, so the SYS module is also required.
Time is used, but we do not need to use the Qtime module, which we provide with QT.
Let's write the import statement section first.
Import SYS from
pyqt4.qtcore import *
Pyqt4.qtgui import *
app = qapplication (SYS.ARGV)
This is a class that controls the lifeblood of the QT program, which we'll explain slowly later. In every QT program, a similar statement is found.
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]"
It contains exception handling, and for an application, we should consider all the anomalies.
Use raise in your program to trigger an exception. Forcing the program into our preconceived orbit.
Obviously, the above program is not enough, it does not have time to judge and GUI part.
How to determine whether the time is up, we give a not particularly accurate method, timing judgment.
Import Time
Qtime.currenttime () < due:
#休眠20秒
time.sleep (20)
When the time is up, we'll jump to the next step, create a form that delays 60s and closes.
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 about the Qtime module.
Now put all the sentences together. Let's test it.
Complete code:
Import sys
import time from
pyqt4.qtcore import *
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.cur Renttime () < due:
Time.sleep (m) seconds
label = Qlabel ("<font color=red size=72><b>" + mes Sage + "</b></font>")
label.setwindowflags (qt.splashscreen)
label.show ()
Qtimer.singleshot (60000, App.Quit) # 1 minute
app.exec_ ()