Using QT to write a very simple graphical Python alarm clock program _python

Source: Internet
Author: User
Tags exception handling in python

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_ ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.