如何在 class 中的 def 之间共享变量?

How to share variable between def in class?

我想把变量 boarddef buttonClicked5(self) 变成 def closeEvent(self, event): 我应该怎么做?

class MainWindow(QtWidgets.QMainWindow):         

    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def buttonClicked5(self):  
        board = Arduino(self.ui.comboBox.currentText())
        communication_start = "Communication Successfully started"
        self.ui.lineEdit_4.setText(communication_start)

    def closeEvent(self, event):
        board.exit()        
            

您可以设置为self.board。像这样:

class MainWindow(QtWidgets.QMainWindow):         

    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def buttonClicked5(self):  
        self.board = Arduino(self.ui.comboBox.currentText())
        communication_start = "Communication Successfully started"
        self.ui.lineEdit_4.setText(communication_start)

    def closeEvent(self, event):
        self.board.exit()