简体   繁体   English

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

[英]How do I use a variable from one class in another?

I understand this question has been asked many times, but I cannot seem to apply it to my code. 我知道这个问题已经被问过很多次了,但是我似乎无法将其应用于我的代码中。 In my code I have a variable called username in the class Ui_MainWindow, which I want to use in the class. 在我的代码中,我要在类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_())

If you answer, can you please explain how you did it, so I can understand. 如果您回答了,请您解释一下您是如何做到的,以便我理解。

Thanks 谢谢

Code copied from OP but shortened to remove unimportant stuff. 从OP复制了代码,但缩短了代码以删除不重要的内容。

Pass the username variable between the UI instances: 在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_())

Search this answer for username to see how it is passed around. 在此答案中搜索username以了解如何传递username

Add this variable to the object attributes by using self : 使用self将这个变量添加到对象属性中:
self.username = self.user_enter.text() . self.username = self.user_enter.text() So you can use it in an other method of the class using self.username . 因此,您可以使用self.username在类的其他方法中使用self.username

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中使用一个变量从一个 class 到另一个变量? - how do I use a variable from one class into another in python? 如何在 Python 中的另一个类中使用一个类中的变量? - How do I use a variable from a class in another class in Python? 如何将变量从一个类转移到另一个类? - How do I carry over a variable from one class to another? 如何将变量从一个 class 调用到另一个? - How do I call a variable from one class to another? 如何在另一个类的一个python类中访问和使用变量 - How do I access and use the variable in one python class in another class 如何从一个类访问变量并在另一个类中使用它? - How can i access a variable from one class and use it in another class? 我如何使用另一个类中的一个类的方法(是否可能) - how do I use a method from one class in another class (is it possible) 如何在同一类的另一个函数中使用一个函数中的一个对象 - How do I use one object from one function in another function in the same class 如何在另一个类的方法中使用来自单独类的一个对象? - How do I use one object from separate class in another class's method? PyQt5:如何将一个按钮的变量输出用于另一个按钮? - PyQt5: how do I use a variable output from one button for another button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM