Advertisement
Guest User

ZeroMQ in PyQt?

a guest
Apr 6th, 2013
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. # Inspiration: http://zetcode.com/gui/pysidetutorial/widgets/
  2. import sys, zmq, infoPack_pb2
  3. from PyQt4 import QtGui, QtCore
  4.  
  5. class Example(QtGui.QWidget):
  6.    
  7.     def __init__(self):
  8.         super(Example, self).__init__()
  9.         self.initUI()
  10.        
  11.         # Set up zeromq send connection
  12.         context = zmq.Context()
  13.         self.publisher = context.socket(zmq.PUB)
  14.         self.publisher.connect('tcp://localhost:2000')
  15.         # Set up zeromq receiver connection
  16.         self.receiver = context.socket(zmq.SUB)
  17.         self.receiver.connect('tcp://localhost:4444')
  18.         self.receiver.setsockopt(zmq.SUBSCRIBE, '')
  19.        
  20.         ## WHERE TO PUT THE ZEROMQ.POLLER() AND WHERE TO RECEIVE THE MESSAGE AND INVOKE THE currentSteerUpdate() METHOD?
  21.        
  22.        
  23.     def initUI(self):      
  24.        
  25.         steeringInstruction = QtGui.QSlider(QtCore.Qt.Horizontal, self)
  26.         steeringInstruction.setTickPosition(QtGui.QSlider.TicksBothSides)
  27.         steeringInstruction.setTickInterval(10)
  28.         steeringInstruction.setFocusPolicy(QtCore.Qt.NoFocus)
  29.         steeringInstruction.setRange(-100, 100)
  30.         steeringInstruction.setGeometry(10, 40, 480, 30)
  31.         steeringInstruction.valueChanged[int].connect(self.sendSteerInstruction)
  32.        
  33.         self.currentSteer = QtGui.QSlider(QtCore.Qt.Horizontal, self)
  34.         self.currentSteer.setFocusPolicy(QtCore.Qt.NoFocus)
  35.         self.currentSteer.setRange(-100, 100)
  36.         self.currentSteer.setGeometry(10, 90, 480, 30)
  37.        
  38.         self.label = QtGui.QLabel('0', self)
  39.         self.label.move(245, 5)
  40.         self.label.setGeometry(245, 5, 30, 30)
  41.        
  42.         self.setGeometry(300, 300, 500, 150)
  43.         self.setWindowTitle('Steering')
  44.         self.show()
  45.        
  46.     def sendSteerInstruction(self, value):
  47.         instructionObj = infoPack_pb2.steeringInstruction()
  48.         instructionObj.steeringInstruction = value
  49.         steeringPb = instructionObj.SerializeToString()
  50.         self.publisher.send(steeringPb)
  51.         self.label.setText(str(value))
  52.         print(value)
  53.    
  54.     def currentSteerUpdate(self, message):
  55.         currentSteerObj = infoPack_pb2.currentSteer()
  56.         currentSteerObj.ParseFromString(message)
  57.         self.currentSteer.setValue(currentSteerObj.currentSteer)
  58.        
  59. def main():
  60.     app = QtGui.QApplication(sys.argv)
  61.     ex = Example()
  62.     sys.exit(app.exec_())
  63.  
  64. if __name__ == '__main__':
  65.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement