-1

How to get a self.count from a class MainWindow to Class Mythread run() count

 board = Arduino("COM6")
 Communication_start= "Communication Successfully started"
 Communication_close = "Communication Close"

 class MainWindow(QtWidgets.QMainWindow):    
     def __init__(self):
         super(MainWindow, self).__init__()
         self.ui = Ui_MainWindow()
         self.ui.setupUi(self)
         self.ui.label.setText('目前次數')
         self.ui.pushButton.setText("Start")
         self.ui.pushButton_2.setText("Stop")
         self.ui.lineEdit.setText("1")
         self.ui.label_2.setText("打點次數")
         self.ui.label_3.setText("上升持續時間")
         self.ui.lineEdit_2.setText("1")
         self.ui.lineEdit_3.setText("1")
         self.ui.label_4.setText("下降持續時間")
         self.ui.lineEdit_4.setText(Communication_start)
         self.ui.toolBar.setWindowTitle("toolBar")
         self.ui.pushButton.clicked.connect(self.buttonClicked)
         self.ui.pushButton_2.clicked.connect(self.buttonClicked2)          
         self.my_thread = MyThread()
         self.my_thread.my_signal_1.connect(self.set_lcdNumber_func)
               
        def buttonClicked(self):                     
            self.my_thread.start()
            count = int(self.ui.lineEdit.text())
            self.ui.lineEdit.setText(str(count))
            self.count = int(self.ui.lineEdit.text()) 
            up_duration = self.ui.lineEdit_2.text()
            self.ui.lineEdit_2.setText(up_duration)
            down_duration = self.ui.lineEdit_3.text()
            self.ui.lineEdit_3.setText(down_duration)        
        def buttonClicked2(self):
            self.ui.lineEdit_4.setText(Communication_close)
            board.exit()
        def set_lcdNumber_func(self, num):   
            self.ui.lcdNumber.display(num)     

    class MyThread(QThread):   

    my_signal_1 = pyqtSignal(str)       

        def __init__(self):
           super(MyThread, self).__init__()                       
       
        def run(self):       
            for i in range(1,int(count)):
                self.my_signal_1.emit(str(i))
                board.digital[8].write(1)
                board.digital[13].write(1)
                time.sleep(float(down_duration))
                board.digital[8].write(0)
                board.digital[13].write(0)
                time.sleep(float(up_duration))
CHL
  • 57
  • 7
  • 1
    Your code has syntax errors, please make sure you have correctly formatted your question. – MatBBastos Nov 12 '21 at 16:34
  • 1
    Then, it seems the problem here isn't really "how to get the variable", but instead "how to specify the Object-Oriented architecture for it to do what I want". You see, for you to "access" something from `MainWindow` in `MyThread.run()`, as it is, you need to have an instance of it. The class itself is not enough, unless you want a static class (which I doubt) – MatBBastos Nov 12 '21 at 16:37
  • 1
    Please, fix the syntax errors in your question, indentation is __crucial__ in Python – MatBBastos Nov 12 '21 at 18:35
  • 1
    There is no reasonable way to propose an answer given the state of the question. (a) it has indentation issues, (b) it is not self contained, as there are `import` statements that are necessary but are not presented, (c) elements such as `board`, `down_duration`, `up_duration` in `MyThread.run()` are undefined – MatBBastos Nov 12 '21 at 18:51

2 Answers2

1

You could create getter/setters to access the variable, and then access it with those methods from the run method.

PS: Your for loop is not indented correctly. All for loops need to be indented for Python to recognize them.

2pichar
  • 1,348
  • 4
  • 17
0

Update answer

class MyThread(QThread): 

my_signal_1 = pyqtSignal(str)       

def __init__(self, window):
    super(MyThread, self).__init__()  
    self.window = window    

def run(self):       
    for i in range(1,int(self.window.count)):
        self.my_signal_1.emit(str(i))
        board.digital[8].write(1)
        board.digital[13].write(1)
        time.sleep(float(down_duration))
        board.digital[8].write(0)
        board.digital[13].write(0)
        time.sleep(float(up_duration)) 

=================================================

class MainWindow(QtWidgets.QMainWindow):
      self.my_thread = MyThread(self)
CHL
  • 57
  • 7