[英]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.