[英]I'm not able to access the attributes of my class?
我正在使用PyQt4模塊創建用戶界面。 我面臨的問題是我無法訪問“self.ftp_tableWidget”變量
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()
這是ftp_tableWidget初始化的地方。
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()
這是更新表小部件的代碼。
def adding(self):
self.msg=add_to()
self.msg.show()
這段代碼調用一個將數據添加到數據庫的類。
class add_to(QtGui.QDialog,Ui_MainWindow):
def __init__(self):
super(add_to,self).__init__()
Ui_MainWindow.__init__(self)
這段代碼初始化了將數據附加到數據庫的類,並調用update_table函數來更新表小部件。
這是我得到的錯誤
self.ftp_tableWidget.setRowCount(count)
AttributeError: 'add_to' object has no attribute 'ftp_tableWidget'
specs:即時通訊使用python 2.7和PyQt4模塊。
根據“notbad jpeg”給出的回答編輯代碼后:類Ui_MainWindow(對象):def init (self,mainwindow):self.setupUi(mainwindow)def setupUi(self,MainWindow):
這給了我一個問題:
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)
這給出了一個錯誤:
MainWindow.setObjectName(_fromUtf8("MainWindow"))
AttributeError: 'Ui_MainWindow' object has no attribute 'setObjectName'
請任何人幫助我。
Thanx提前
該評論說明了您的問題。 您需要在調用self.setupUi()
Ui_MainWindow
類中添加__init__()
方法,以便在調用super()
時立即調用setupUi()
中的所有內容。
編輯:所以現在你改變了你的代碼並且你得到__init__()
接受兩個參數錯誤,我環顧四周看到你的內聯代碼顯示你需要將一個MainWindow
參數傳遞給你的Ui_MainWindow.__init__(self, MainWindow)
。 然后,將MainWindow
傳遞給您的setupUi
方法。
編輯#2:改變
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)
至...
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.MainWindow=Ui_MainWindow(self.window)
。 另外,您將自己的實例作為參數傳遞給自己。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.