Pyqt qtimer and qthread examples

Source: Internet
Author: User
Tags time 0

#-*-Coding: UTF-8 -*-

# Python: 2.x

_ Author _ = 'admin'

 

From pyqt4.qtgui import *

From pyqt4.qt import *

From pyqt4.qtcore import *

From start import ui_form

Import sys

Class Example (qdialog, ui_form ):

Def _ init _ (self, parent = none ):

Super (example, self). _ init _ (parent)

Self. setupui (Self)

 

# Border Style

Self. timenum. setframestyle (qframe. styledpanel |

Qframe. Plain)

Self. Time = qtimers ()

Self. Start. clicked. Connect (self. start1)

Self. Stop. clicked. Connect (self. Stop1)

Self. Connect (self. Time, signal ("updatetime ()")

, Self. updatetime)

Self. setwindowflags (QT. windowminimizebuttonhint) # Do not change the window size.

Def updatetime (Self ):

Self. timenum. settext (U' time is {0} '. Format (qstring. Number (self. Num )))

Self. Num + = 1

Def Stop1 (Self ):

Self. Time. Stop1 ()

Self. Stop. setenabled (false)

Self. Start. setenabled (true)

Self. timenum. settext (U' enter the time 0 second ')

Def start1 (Self ):

Self. num = 0

Self. Start. setenabled (false)

Self. Stop. setenabled (true)

Self. Time. Start ()

From time import *

Class qtimers (qthread ):

Def _ init _ (self, parent = none ):

Super (qtimers, self). _ init _ (parent)

Self. stoped = false

Self. mutex = qmutex () # provides sequential access between threads

Def run (Self ):

With qmutexlocker (self. mutex): # qmutexlocker (qmutex * mutex)

Self. stoped = false

While true:

If self. stoped:

Return

Self. emit (signal ('updatetime () ') # Send signal

Sleep (1)

Def Stop1 (Self ):

With qmutexlocker (self. mutex ):

Self. stoped = true

Def isstoped (Self ):

With qmutexlocker (self. mutex ):

Return self. stoped

 

 

 

APP = qapplication (SYS. argv)

Qapplication. setquitonlastwindowclosed (false)

X = example ()

X. Show ()

Sys.exit(app.exe C _())

:

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.