Today we'll talk about the GUI applet written in Python. A small alarm clock (just a ScreenTip, no sound)
Let us first introduce how this alarm clock is wonderful.
Need 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 must be asked, why do you want to do such a weak explosion of the program it. Obviously, education is more important than its practical use.
Like other modules, the QT Interface Pack needs to be loaded.
We used the command line input, so the SYS module is also required.
Time is used, but we do not need to use the timing module, we use QT to provide the Qtime module.
Let's write the import statement section first.
Import sysfrom pyqt4.qtcore import *from pyqt4.qtgui Import *app = Qapplication (SYS.ARGV)
This is a class that controls the lifeblood of the QT program, and then we'll explain it slowly. In each QT program, a similar statement is found.
Then it's time to enter the alarm. 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 take all the exceptions into account.
The program uses raise to trigger an exception. Forcing the program into our desired trajectory.
Obviously, the above program is not enough, it does not have time to judge and GUI part.
How to tell if the time is up, we give a not particularly accurate method, timing judgment.
Import Timewhile Qtime.currenttime () < due: #休眠20秒 time.sleep (20)
When the time is up, we'll jump to the next step, create a form, and turn it off after 60s delay.
Label = Qlabel ("" + message + "") label.setwindowflags (Qt.splashscreen) label.show () Qtimer.singleshot (60000 , app.quit) # 1 minuteapp.exec_ ()
It seems that we need to write an introduction about the Qtime module.
Now put all the sentences together. Let's test it out.
Complete code is included:
Import sysimport timefrom pyqt4.qtcore import *from pyqt4.qtgui Import *app = qapplication (SYS.ARGV) Try: message = "Al ert! " 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 Clockwhile qtime.currenttime () < du E: time.sleep # Secondslabel = Qlabel ("+ message +"") label.setwindowflags (Qt.splashscreen) Label.show () Qtimer.singleshot (60000, App.Quit) # 1 minuteapp.exec_ ()