属性错误:对象在python中没有属性

时间:2017-01-08 23:12:19

标签: python object attributes

我有以下类Ui_MainWindow(对象)。但是我得到属性错误,AttributeError: 'Ui_MainWindow' object has no attribute 'ser' .ser在check_phone()方法中定义之前。 sendMessage()方法中出现此问题。 为什么.ser不再被认出了?

    from PyQt5 import QtCore, QtGui, QtWidgets

    import Tkinter as tk
    import tkFileDialog as filedialog
    import tkMessageBox
    import serial
    import time

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(503, 486)
        self.pushButton_2 = QtWidgets.QPushButton(self.widget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayout.addWidget(self.pushButton_2, 3, 2, 1, 1)
        self.pushButton_2.clicked.connect(self.send_sms) #send sms function

    def check_phone(self):
         ser = serial.Serial('/dev/ttyACM0', 
                 460800, 
                 timeout=5, 
                 xonxoff = False,   
                 rtscts = False, 
                 bytesize = serial.EIGHTBITS, 
                 parity = serial.PARITY_NONE, 
                 stopbits = serial.STOPBITS_ONE)

    def sendMessage(self):
        self.ser.write('ATZ\r')
        time.sleep(1)
        self.ser.write('AT+CMGF=1\r')
        time.sleep(1)
        self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''')
        time.sleep(1)
        self.ser.write(self.content + "\r")
        time.sleep(1)
        self.ser.write(chr(26))
        time.sleep(2)

    def send_sms(self):
    check = self.radioButton.isChecked() #stuff doesnt work yet!
    test =  self.lineEdit.text()
    print(test)
    if check == True:
        if not self.lineEdit.text():
            root = tk.Tk()
            root.withdraw()
            tkMessageBox.showwarning("Phone Number Missing!", "Please enter a valid phone number")
            root.destroy()
            root.mainloop()
            return
        if not self.plainTextEdit.toPlainText():
            root = tk.Tk()
            root.withdraw()
            tkMessageBox.showwarning("Message Missing!", "Please enter a text message")
            root.destroy()
            root.mainloop()
            return
        else:
            sms = Ui_MainWindow(self.lineEdit.text(), self.plainTextEdit.toPlainText())
            sms.check_phone()
            sms.sendMessage()
            sms.disconnectPhone()
            self.plainTextEdit2.setText('message sent successfully')

1 个答案:

答案 0 :(得分:2)

正如Jon指出的那样,您将serial.Serial分配给check_phone中的(很快丢弃的)本地变量,而不是实例属性。

替换它:

def check_phone(self):
     ser = serial.Serial('/dev/ttyACM0', 
             460800, 
             timeout=5, 
             xonxoff = False,   
             rtscts = False, 
             bytesize = serial.EIGHTBITS, 
             parity = serial.PARITY_NONE, 
             stopbits = serial.STOPBITS_ONE)

有了这个:

def check_phone(self):
     self.ser = serial.Serial('/dev/ttyACM0', 
             460800, 
             timeout=5, 
             xonxoff = False,   
             rtscts = False, 
             bytesize = serial.EIGHTBITS, 
             parity = serial.PARITY_NONE, 
             stopbits = serial.STOPBITS_ONE)