Python 如何从另一个类方法继承变量
朋友们好。你看到的代码是我毕业设计的一部分。Python 如何从另一个类方法继承变量,python,class,variables,inheritance,Python,Class,Variables,Inheritance,朋友们好。你看到的代码是我毕业设计的一部分。 我需要从CrateAcc类中获取变量,以便用之前装箱的帐户的相同名称编写Firebase服务器。在度量类中,我需要获取“tc”和“d1”变量。您应该将所需的变量传递给度量构造函数。因此,类定义应该是这样的: class CreateAcc(QDialog): def __init__(self): super(CreateAcc,self).__init__() loadUi("createacc.ui",sel
我需要从CrateAcc类中获取变量,以便用之前装箱的帐户的相同名称编写Firebase服务器。在度量类中,我需要获取“tc”和“d1”变量。您应该将所需的变量传递给度量构造函数。因此,类定义应该是这样的:
class CreateAcc(QDialog):
def __init__(self):
super(CreateAcc,self).__init__()
loadUi("createacc.ui",self)
self.confirmacc.clicked.connect(self.createaccfunction)
self.password.setEchoMode(QtWidgets.QLineEdit.Password)
self.confirmpass.setEchoMode(QtWidgets.QLineEdit.Password)
self.invalid.setVisible(False)
def createaccfunction(self):
email = self.email.text()
tc = email
now = datetime.now()
d1 = now.strftime("%Y %m %d %H %M")
if self.password.text()==self.confirmpass.text() and len(email)>=11: #exception won't work here!!#
password = self.password.text()
#datatc = {email : }
#datapass = {"password" : password}
db.child("Registered_Users").child(tc).child(d1)
#db.child("Registered_Users").child("HMI_USER").child("HMI").push(datapass)
login = Login()
widget.addWidget(login)
widget.setCurrentIndex(widget.currentIndex() + 1)
else:
self.invalid.setVisible(True)
class Measure(QDialog):
def __init__(self):
super(Measure,self).__init__()
loadUi("measure.ui",self)
widget.setFixedWidth(1022)
widget.setFixedHeight(744)
self.bodytmpBtn.clicked.connect(self.bodyTemp)
def bodyTemp(self):
i2c = io.I2C(board.SCL, board.SDA, frequency=100000)
mlx = adafruit_mlx90614.MLX90614(i2c)
target_temp = "{:.2f}".format(mlx.object_temperature)
datatemp = {CreateAcc.d1 : target_temp}
db.child("Registered_Users").child(CreateAcc.tc).child(CreateAcc.d1).push(datatemp)
然后,要传递参数:
class Measure(QDialogue):
def __init__(self, tc, d1, *args, **kwargs):
super(Measure, self).__init__(*args, **kwargs)
self.tc = tc
self.d1 = d1
# do other stuff
将这些变量传递给度量构造函数?@Neb”类度量(QDialog,CreateAcc):“像这样吗?不,请看下面我的答案。”
acc = CreateAcc()
acc.createaccfunction()
# here you instantiate the new object passing those parameters
measure = Measure(acc.tc, acc.d1)