I'm not able to access the attributes of my class?

I'm creating a User Interface using PyQt4 module . the problem i'am facing is that i'm not able to access "self.ftp_tableWidget" variable

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(790, 610)
    self.FTP = QtGui.QWidget()
    self.FTP.setObjectName(_fromUtf8("FTP"))
    self.ftp_tableWidget = QtGui.QTableWidget(self.FTP)
    self.ftp_tableWidget.setGeometry(QtCore.QRect(40, 90, 411, 192))
    self.ftp_tableWidget.setMinimumSize(QtCore.QSize(331, 0))
    self.ftp_tableWidget.setObjectName(_fromUtf8("ftp_tableWidget"))
    self.ftp_tableWidget.setColumnCount(2)
    self.ftp_tableWidget.setRowCount(31)
    item = QtGui.QTableWidgetItem()
    self.ftp_tableWidget.setHorizontalHeaderItem(0, item)
    item = QtGui.QTableWidgetItem()
    self.ftp_tableWidget.setHorizontalHeaderItem(1, item)
    self.update_table()

This where the ftp_tableWidget is intialized.

  def update_table(self):
    cursor.execute('''SELECT MAX(SNO) FROM ftp_auth_table1''')
    entry=cursor.fetchall()
    entry=entry[0]
    count=entry[0]
    self.ftp_tableWidget.setRowCount(count)
    cursor.execute('''SELECT * FROM ftp_auth_table1''')
    entry=cursor.fetchall()

This is the code that updates the table widget.

 def adding(self):
    self.msg=add_to()
    self.msg.show()

This piece of code is calling a class which adds data to the database.

class add_to(QtGui.QDialog,Ui_MainWindow):
def __init__(self):
    super(add_to,self).__init__()
    Ui_MainWindow.__init__(self)

This piece of code is initializes the class which appends data to the database and calls the update_table function to update the table widget .

This the Error that im getting

    self.ftp_tableWidget.setRowCount(count)
    AttributeError: 'add_to' object has no attribute 'ftp_tableWidget'  

specs : im using python 2.7 and PyQt4 module .

after editing the code according to the answere given by "notbad jpeg": class Ui_MainWindow(object): def init (self,mainwindow): self.setupUi(mainwindow) def setupUi(self, MainWindow):

this is giving me a problem :

class add_to(QtGui.QDialog,Ui_MainWindow):
   def __init__(self):
    super(add_to,self).__init__()
    self.window=QtGui.QMainWindow()
    self.MainWindow=Ui_MainWindow(self.window)
    Ui_MainWindow.__init__(self,self.MainWindow)

this gives an error:

MainWindow.setObjectName(_fromUtf8("MainWindow"))
AttributeError: 'Ui_MainWindow' object has no attribute 'setObjectName'

Pls can anyone help Me .

Thanx in advance


That comment is stating your problem. You need to add an __init__() method to your Ui_MainWindow class that calls self.setupUi() so that everything in your setupUi() gets called right away when you call super() .

Edit: So now that you changed your code a bit and you're getting the __init__() takes two arguments error, I looked around and saw your inline code that shows that you need to pass a MainWindow argument to your Ui_MainWindow.__init__(self, MainWindow) . And then, pass that MainWindow to your setupUi method.

Edit #2: Change

class add_to(QtGui.QDialog,Ui_MainWindow):
   def __init__(self):
       super(add_to,self).__init__()
       self.window=QtGui.QMainWindow()
       self.MainWindow=Ui_MainWindow(self.window)
       Ui_MainWindow.__init__(self,self.MainWindow)

to...

class add_to(QtGui.QDialog,Ui_MainWindow):
   def __init__(self):
       super(add_to,self).__init__()
       self.window=QtGui.QMainWindow()
       self.MainWindow=Ui_MainWindow(self.window)

The call to Ui_MainWindow.__init__() was redundant, since it gets called automatically when you create and instance of the class: self.MainWindow=Ui_MainWindow(self.window) . Plus, you were passing an instance of itself as an argument to itself.

链接地址: http://www.djcxy.com/p/65020.html

上一篇: ImportError:我的Raspberry Pi没有名为PyQt4的模块

下一篇: 我无法访问我的课程的属性?