【问题标题】:How to inherit a variable from another classes method如何从另一个类方法继承变量
【发布时间】:2021-05-22 13:07:56
【问题描述】:
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)

朋友们好。你看到的代码是我毕业设计的一部分。 我需要从 CrateAcc 类中获取变量,以便以与之前创建的帐户相同的名称编写我的 Firebase 服务器。在度量类中,我需要获取“tc”和“d1”变量。

【问题讨论】:

  • 将这些变量传递给 Measure 构造函数?
  • @Neb 'class Measure(QDialog,CreateAcc):' 这样?
  • 不,请看下面我的回答

标签: python class variables inheritance


【解决方案1】:

您应该将所需的变量传递给 Measure 构造函数。因此,类定义应该是这样的:

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 

然后,传递参数:

acc = CreateAcc()
acc.createaccfunction()

# here you instantiate the new object passing those parameters
measure = Measure(acc.tc, acc.d1)

【讨论】:

    猜你喜欢
    • 2018-11-22
    • 1970-01-01
    • 2021-11-30
    • 2021-02-02
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    相关资源
    最近更新 更多