如何使用另一类中的一个变量?

时间:2019-04-12 12:15:22

标签: python sqlite pyqt5

我知道这个问题已经问过很多遍了,但是我似乎无法将其应用于我的代码中。在我的代码中,我要在类Ui_MainWindow中使用一个名为username的变量。

from viewAllRooms import Ui_ViewAllRooms
from teacher_menu import Ui_Menu
from viewAllRooms import Ui_ViewAllRooms
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Menu(QtWidgets.QMainWindow, Ui_Menu):
    def __init__(self, parent=None):
        super(Ui_Menu, self).__init__(parent)
        self.setupUi(self)
        self.viewAll = Ui_ViewAllRooms()
        self.viewall_button.clicked.connect(self.viewAll.show)
        self.viewall_button.clicked.connect(self.hide)

class Ui_ViewAllRooms(QtWidgets.QMainWindow, Ui_ViewAllRooms):
    def __init__(self, parent=None):
        super(Ui_ViewAllRooms, self).__init__(parent)
        self.setupUi(self)
        self.book_Button.clicked.connect(self.book_clicked)

    @QtCore.pyqtSlot()
    def book_clicked(self):
        self._checked_items = []
        self.dialogBook.show()
        self.dialogBook.yes_button.clicked.connect(self.addBooking)

    def addBooking(self):
        now = QDate.currentDate()
        now1 = now.toString(Qt.DefaultLocaleLongDate)
        #I want to use the username here#

class Ui_MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def loginCheck(self):
        username = self.user_enter.text()
        #I have code here which connects to database to check username details
        #If the username is found in database then:
            self.hide()
            self.teacherMenu = Ui_Menu()
            self.teacherMenu.show()

    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.loginCheck()
        self.login_button.clicked.connect(self.loginCheck)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Ui_MainWindow()
    w.show()
    sys.exit(app.exec_())

如果您回答了,请您解释一下您的操作方式,以便我理解。

谢谢

2 个答案:

答案 0 :(得分:1)

从OP复制的代码,但缩短了代码以删除不重要的内容。

在UI实例之间传递username变量:

class Ui_Menu(QtWidgets.QMainWindow, Ui_Menu):
    def __init__(self, username, parent=None):
        super(Ui_Menu, self).__init__(parent)
        self.setupUi(self)
        self.viewAll = Ui_ViewAllRooms(username)
        self.viewall_button.clicked.connect(self.viewAll.show)
        self.viewall_button.clicked.connect(self.hide)

class Ui_ViewAllRooms(QtWidgets.QMainWindow, Ui_ViewAllRooms):
    def __init__(self, username, parent=None):
        super(Ui_ViewAllRooms, self).__init__(parent)
        self.username = username
        self.setupUi(self)
        self.book_Button.clicked.connect(self.book_clicked)
        #-----------#

    def addBooking(self):
        now = QDate.currentDate()
        now1 = now.toString(Qt.DefaultLocaleLongDate)
        #Use self.username here#

class Ui_MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def loginCheck(self):
        username = self.user_enter.text()
        #I have code here which connects to database to check username details
        #If the username is found in database then:
            self.hide()
            self.teacherMenu = Ui_Menu(username)
            self.teacherMenu.show()

    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.loginCheck()
        self.login_button.clicked.connect(self.loginCheck)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Ui_MainWindow()
    w.show()
    sys.exit(app.exec_())

在此答案中搜索username,以了解它是如何传递的。

答案 1 :(得分:0)

使用self将此变量添加到对象属性中:
self.username = self.user_enter.text()。因此,您可以使用self.username在类的其他方法中使用它。