-3

I need get a variables self.countup_durationdown_duration from a class MainWindow to Class Mythread run() , How to do ?

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))     
Barmar
  • 741,623
  • 53
  • 500
  • 612
CHL
  • 57
  • 7
  • Does this answer your question? [How to get variable from Class MainWindow to Class Mythread](https://stackoverflow.com/questions/69945889/how-to-get-variable-from-class-mainwindow-to-class-mythread) – wovano Nov 13 '21 at 09:44
  • Welcome to Stack Overflow. Please don't ask the same question twice. Read [How do I ask a good question?](//stackoverflow.com/help/how-to-ask) to learn how you could improve your question and get better answers instead. – wovano Nov 13 '21 at 09:46
  • Open new issue because provide more detail – CHL Nov 13 '21 at 10:48
  • 1
    You should never create a new question for that. Instead, edit and improve the original question. There are many discussions about this on meta (see [here](https://meta.stackexchange.com/search?q=repost+question)). – wovano Nov 13 '21 at 11:04
  • 1
    @ChenAndy also follow the [tour] and read [ask], avoid unnecessary formatting and remember that questions should be self contained, meaning that if you provide code it should be in form of a [mre], possibly including all required imports, especially if they are not standard/known modules, like the `Ui_MainWindow`, which you ***did not*** provide (and which could also be avoided, if you *actually* try to provide a MRE). – musicamante Nov 13 '21 at 14:28

1 Answers1

1

You can pass the MainWindow object as a parameter to MyThread.

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))                  

In the MainWindow class you do:

        self.my_thread = MyThread(self)
Barmar
  • 741,623
  • 53
  • 500
  • 612