Recently I want to write something similar to an automated simulated test. Some of them are countdown settings, and the answer is stopped when the time is up. Each unit is implemented separately and finally assembled together. (It is implemented separately to facilitate debugging. I personally think ). It took more than an hour to design and write code and debug for more than an hour. Hey hey...
Implementation platform: pycharm 3.4.1 python2.7.8 qt4.8 pyside
Wang Dao first: (declare that this is only a demo, only display the countdown timer, and others are ignored). The main control used is qtgui. qlcdnumber.
Initial interface:
Menu Bar: (1) set countdown: Set the countdown time (1 ~ 100)
(2) start testing: start the test, that is, start the countdown
(3) pause testing: Pause the timer by clicking the pause button and continue the timer again. It is the same as the pause key of the music player.
(4) stop testing: the test is stopped halfway. The countdown is 0.
Paste the code below and write your thoughts on the code.
From pyside import qtgui, qtcoreoriginal_time = 0 class mainwindow (qtgui. qmainwindow): def _ init _ (Self): Super (mainwindow, self ). _ init _ () self. time = original_time self. timer = none self. ispause = false self. creatmenubar () self. creattoolbar () self. creatactions () self. creatcountdown () self. resize (300,400) def creatmenubar (Self): menubar = self. menubar () self. filemenu = menubar. addmenu ('file') self. setmenubar (menubar) def creattoolbar (Self): Pass def creatactions (Self): Self. setcountdown = qtgui. qaction ('set countdown ', self, triggered = self. changecountdown) self. changebackground = qtgui. qaction ('change background ', self, triggered = self. setbackground) self. starttesting = qtgui. qaction ('start testing ', self, triggered = self. starttestset) self. closetesting = qtgui. qaction ('Stop testing ', self, triggered = self. stoptest) self. pausetesting = qtgui. qaction ('pause testing', self, triggered = self. pausetest) self. filemenu. addaction (self. setcountdown) self. filemenu. addaction (self. changebackground) self. filemenu. addaction (self. starttesting) self. filemenu. addaction (self. pausetesting) self. filemenu. addaction (self. closetesting) def changecountdown (Self): # create the countdown control if self. timer is none: Self. time, OK = qtgui. qinputdialog. getinteger (self, "set countdown", "percentage:", 45, 0,120, 1) # obtain the input dialog box. Here, 45 is the value set in the dialog box if OK: if self. time <= 0 or self. time & gt; 100: qtgui. qmessagebox. warning (self, "warning message", '\ nplease enter a integer I like: 0 <I <= 100') self. changecountdown () # There is a recursive feeling here. If the input value does not meet the requirements, the prompt dialog box appears, and the input dialog box else: Self is displayed again. updatetime () # update the display time else: qtgui on the interface after the initial value is set. qmessagebox. warning (self, "warning message", "\ n please stop the testing first before you reset the countdown ")
# If you need to reset the initial value during the countdown process, the system prompts "please stop the test before resetting the countdown" def updatetime (Self): Zero = ''zerodouble = ': 00 'If self. time <10: Zero = '0' if self. time = 100: zerodouble = ': 0' self. countdown. display (zero + STR (self. time) + zerodouble) def setbackground (Self): Pass def starttestset (Self): # This is a module from the beginning to the countdown if self. timer: Self. timer. stop () Zero = ''zerodouble = ': 00' if self. time <10: Zero = '0' if self. time = 100: ZER Odouble = ': 0' self. countdown. display (zero + STR (self. time) + zerodouble) If self. getnumber (): Self. timer = qtcore. qtimer (Self) self. timer. timeout. connect (self. timereset) # change the display time remaining for each second. timer. start (1000) def getnumber (Self): Self. minnumber = self. time self. secondnumber = 0 if self. minnumber = 0: qtgui. qmessagebox. warning (self, "warning message", "\ nplease set countdown first! ") Return false return true def timereset (Self): Self. zeromin = ''self. zerosecond = ''if self. secondnumber = 0: Self. secondnumber = 59 If self. minnumber = 0: # When the minute and second digits are both 0, it indicates that the time has reached, and the test is stopped. The prompt dialog box self is displayed. stoptest () qtgui. qmessagebox. warning (self, "warning message", "\ n game over! \ Nplease wait for your record. \ t ") return else: Self. minnumber-= 1 else: Self. secondnumber-= 1 If self. minnumber <10: Self. zeromin = '0' if self. secondnumber <10: Self. zerosecond = '0' self. countdown. display (self. zeromin + STR (self. minnumber) + ':' + self. zerosecond + STR (self. secondnumber) def stoptest (Self): Try: Self. timer. stop () failed T: Pass self. countdown. display ('0' + STR (original_time) + ": 00") self. timer = none self. time = 0 def pausetest (Self): # This module that pauses Timing If self. timer: If self. ispause: # because the first press the pause key is paused, and the second press the pause key to continue, a flag: Self is required. ispause to change the status self at any time. ispause = false else: Self. ispause = true if self. ispause: Self. timer. stop () self. countdown. display (self. zeromin + STR (self. minnumber) + ':' + self. zerosecond + STR (self. secondnumber) else: Self. timer = qtcore. qtimer (Self) self. timer. timeout. connect (self. timereset) self. timer. start (1000) else: Return def creatcountdown (Self): Self. countdown = qtgui. qlcdnumber () self. countdown. display ('0' + STR (self. time) + ": 00") self. edit = qtgui. qtextedit () self. label = qtgui. qlabel ('count low') # self. label. self. splitter = qtgui. qsplitter (qtcore. QT. vertical) splitterh = qtgui. qsplitter () splitterh. addwidget (self. label) splitterh. addwidget (self. countdown) self. splitter. addwidget (splitterh) self. splitter. addwidget (self. edit) self. setcentralwidget (self. splitter) If _ name _ = '_ main _': Import sys APP = qtgui. qapplication (sys. argv) mainwindow = mainwindow () mainwindow. show () sys.exit(app.exe C _())
Notes:
The normal design case sequence is: setcountdown -- starttesting -- (pause/stop)
However, in some cases, the buttons may be messy:
(1) If you have not set the initial value, start testing. timer assigns the initial value none in init, and checks whether the timer is enabled in start testing. If the initial value is not enabled, the set value is obtained. If it is enabled, stop timer and display the preset initial values on the page. Start the countdown.
(2) The most important thing is to disable timer and set self. Time to zero in stop.
(3) As long as your timer is running and you want to reset the initial value, the system will prompt: "Please end the test first and then set the initial value ".
(4) In fact, no matter whether the key is pressed or not, it is OK as long as the function that violates each key is well judged.
Disadvantages:
(1) the disadvantage of the lcdnumber module is that only five digits can be displayed. Therefore, to remove ":", only four digits are left, and then two digits of the second are removed, the maximum value displayed is 99 minutes ....
(2) Not very nice, haha .. There are other functions to be discovered.
LCD countdown-notes