title: Python PyQt4 Threading, sharing variables
body: <p>I have been researching this topic for the past few days and have not found a suitable answer for my application. I am building a GUI in python 2.7 with PyQt4 to interact with a robot. I need one thread(main) to run the GUI created with QT Designer and another thread(worker) to continuously transmit data to and from the robot. I want to use data entered in the GUI(main thread) in the infinite worker thread. In all of the tutorials I have seen, data is only sent from the worker to the main thread; like the usual "loading bar" example. </p>
  
  <p>This is what I have so far. This program imports the GUI output from QT Designer and prints values to the console when they are pressed. I also have an infinite thread set up that prints "hello" to the console which does work. Just to get me pointed in the right direction, can you show me how to change the infinite print "hello" to something else when the the button called "pushbutton" is clicked?  I am familiar with C/C++ but this is my first Python project and any help is appreciated.</p>
  
  <pre><code>from PyQt4 import QtGui, QtTest, QtCore # Import the PyQt4 module we'll need
  import sys # We need sys so that we can pass argv to QApplication
  
  import gui # Import the GUI output from QT Designer
  
  
  class GUI(QtGui.QMainWindow, gui.Ui_MainWindow):  # This class is to interact         with the GUI
  def __init__(self):
      super(self.__class__, self).__init__()
      self.setupUi(self)  
      self.threadclass = ThreadClass()
      self.threadclass.start() # start thread class
  
      #########  Binds GUI widgets with functions  #####
      self.connectbutton.clicked.connect(self.pushbutton) # Call function "pushbutton" when pressed
      self.motor_up.clicked.connect(self.motorup) # Call function motorup when pressed
      self.motor_down.clicked.connect(self.motordown) # Call function motorup when pressed
      self.motor_speed_slider.valueChanged.connect(self.slider) # Call function slider
      self.auto_manual_check.stateChanged.connect(self.checkbox) # Call function checkbox
      self.Kp_box.valueChanged.connect(self.kp_entry)
      self.Ki_box.valueChanged.connect(self.ki_entry)
      self.Kd_box.valueChanged.connect(self.kd_entry)
  
  
      ######### Functions to run when GUI event is encountered
  
  def pushbutton(self): # stuff to do if connect button is pressed                ## THIS BUTTON
      self.connectlabel.setText("Connected")
      QtTest.QTest.qWait(2000)        
      self.connectlabel.setText("Disconnected")
      self.emit(SIGNAL("text(Qstring)"),"it worked")
  
  def slider(self): # Stuff to do when slider is changed
      print(self.motor_speed_slider.value()) # Print to monitor
  
  def motorup(self): # Run if motor up is clicked
      print("motor up")
  
  def motordown(self): # Run if motor down is clicked
      print("motor down")
  
  def checkbox(self): # Run if checkbox is changed
      if self.auto_manual_check.isChecked():
          print("is checked")
      else:
          print("unchecked")
  
  def kp_entry(self): # Run if kp is changed
      print(self.Kp_box.value())
  
  def ki_entry(self): # Run if ki is changed
      print(self.Ki_box.value())
  
  def kd_entry(self):# Run if kd is changed
      print(self.Kd_box.value())
  
  
  
  class ThreadClass(QtCore.QThread): # Infinite thread to communicate with  robot
  def __init__(self, parent = None):
      super(ThreadClass, self).__init__(parent)
  
  def run(self):
      while 1:            # Runs continuously 
          print "hello"   #                           CHANGE THIS WHEN pushbutton IS PRESSED
  
  
  
  
  def main():  # Function to run the main GUI
  app = QtGui.QApplication(sys.argv)  # A new instance of QApplication
  form = GUI()                 # We set the form to be our ExampleApp 
  form.show()                         # Show the form
  app.exec_()                         # and execute the app
  
  
  if __name__ == '__main__':              # if we're running file directly and not importing it
  main()                              # run the main function
  </code></pre>
  
  <p><strong>EDIT:</strong>
  Thank you for the quick and detailed responses. After experimenting with your solutions I think I'm getting closer, however I'm not getting them to work for my application, I think I may have been unclear in my initial question. I am essentially trying to pass variables between threads, the actual printing was just to show that the variable was successfully transferred. I think this stripped down code will clear it up. </p>
  
  <pre><code>from PyQt4 import QtGui, QtTest, QtCore # Import the PyQt4 module we'll need
  import sys # We need sys so that we can pass argv to QApplication
  
  import gui # Import the GUI output from QT Designer
  
  
  class GUI(QtGui.QMainWindow, gui.Ui_MainWindow):            # This class is to interact with the GUI
  def __init__(self):
      super(self.__class__, self).__init__()
      self.setupUi(self)  
      self.threadclass = ThreadClass()
      self.threadclass.start() 
      self.Kp_box.valueChanged.connect(self.kp_entry)     # Call function kp_entry if box value is changed
  
  def kp_entry(self):                                     # Run if kp is changed                  
      print(self.Kp_box.value())                          # I WANT TO SEND THE BOX VALUE TO THE WORKER THREAD        
  
  class ThreadClass(QtCore.QThread):                          # Infinite thread to communicate with robot
  def __init__(self, parent = None):
      super(ThreadClass, self).__init__(parent)
  
  def run(self):
      while 1:             
          print self.Kp_box.value()                       #CONTINUOUSLY PRINT THE UPDATED BOX VALUE    
  
  
  def main():                             # Run the main GUI
     app = QtGui.QApplication(sys.argv)  # A new instance of QApplication
     form = GUI()                        # We set the form to be our ExampleApp 
     form.show()                         # Show the form
     app.exec_()                         # and execute the app
  
  if __name__ == '__main__':              # if we're running file directly and not importing it
  main()                              # run the main function
  </code></pre>
  
  <p>All I want to do is enable the user to update a value in the GUI and then pass that value to the worker thread. In this case, I have an entry box so the user can change the number. I then need the program to transfer that number to the worker thread which will communicate with the robot. Right now the code does not work, I just got it as close as I could to better show what I'm trying to do.</p>
  
  <p>If it helps, this is my GUI<a href="https://i.stack.imgur.com/qyeGm.jpg" rel="nofollow noreferrer">here</a></p>