Skip to content

Instantly share code, notes, and snippets.

@asyd
Last active February 7, 2017 22:05
Show Gist options
  • Save asyd/349027e80d8279151ce3bae432017870 to your computer and use it in GitHub Desktop.
Save asyd/349027e80d8279151ce3bae432017870 to your computer and use it in GitHub Desktop.
Qt4 timer example #2
#!/usr/bin/env python
# encoding: utf-8:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.uic import *
import random
class GetTemp(QObject):
message = pyqtSignal(dict)
def __init__(self):
QObject.__init__(self)
# self.message = pyqtSignal(str)
def get_values(self):
data = {'bed': str(random.randint(50, 60)),
'tool0': str(random.randint(180, 220))}
self.message.emit(data)
class App(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
# Charge de la définition de l'UI depuis le fichier produit par le designer
self.ui = loadUi("example.ui")
self.ui.show()
# Création d'une instance de GetTemp
self.get_status = GetTemp()
# On connecte le message émis par GetTemp à la méthode update_temp
self.get_status.message.connect(self.update_temp)
# Créatiion d'un QTimer
self.timer = QTimer()
# Choix de l'intervalle
self.timer.setInterval(1000)
# On appelle la méthode get_values de l'instance de GetStatus quand le timer arrive à expiration
self.timer.timeout.connect(self.get_status.get_values)
# Démarrage du timer
self.timer.start()
def update_temp(self, data):
self.ui.bedTemperature.setText(data['bed'])
self.ui.tool0.setText(data['tool0'])
if __name__ == '__main__':
app = QApplication(sys.argv)
win = App()
app.exec_()
"""
$ cat example.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="bedTemperature">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>66</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>10</string>
</property>
</widget>
<widget class="QLabel" name="tool0">
<property name="geometry">
<rect>
<x>20</x>
<y>40</y>
<width>66</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>10</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>32</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment