Advertisement
furas

Python - threading

May 16th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. # --- versiom 1 ---
  2.  
  3.         self.pushButton.clicked.connect(self.function1)
  4.         QtCore.QMetaObject.connectSlotsByName(Form)
  5.         self.ser = serial.Serial('COM7', 9600, timeout=0,parity = serial.PARITY_NONE)
  6.  
  7.     def function1(self):
  8.         threading.Thread(target=self.update_text).start()
  9.        
  10.     def update_text(self):
  11.         s = self.ser.read(10)
  12.         self.textBrowser.append(s.decode())
  13.        
  14. # --- version 2 ---
  15.  
  16.         self.update_thread = None # default value at start      
  17.         self.pushButton.clicked.connect(self.function1)
  18.         QtCore.QMetaObject.connectSlotsByName(Form)
  19.         self.ser = serial.Serial('COM7', 9600, timeout=0, parity=serial.PARITY_NONE)
  20.  
  21.     def function1(self):
  22.         # check if thread already run
  23.         if not self.update_thread:
  24.             # run thread
  25.             self.update_thread = threading.Thread(target=self.update_text)
  26.             self.update_thread.start()
  27.        
  28.     def update_text(self):
  29.         s = self.ser.read(10)
  30.         self.textBrowser.append(s.decode())
  31.         # set None to inform main thread that this thread is over
  32.         self.update_thread = None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement