我试图在按钮点击时在我的主窗口旁边打开第二个窗口,并在其上显示一个带有一些数据的tablewidget。当我打开窗户时,它会升起
AttributeError: 'Ui_MainWindow' object has no attribute 'openTable'
主窗口是使用Qt Designer创建的,并使用pyuic4转换了ui文件。如何正确执行此操作以便不会引发错误?
按钮调用功能:
def showCliplist(self):
data = self.metadata_list
luts = self.lutlist
selected_lut = self.LUTBox.currentIndex()
openTable = ClipListViewer(data,luts,selected_lut)
self.openTable.show()
新窗口类:
class ClipListViewer(QtGui.QWidget):
def __init__(self, data, luts, selected_lut, parent = None):
super(ClipListViewer,self).__init__()
self.setWindowTitle('Cliplist')
self.resize(900,600)
self.metadata = data
self.curentluts = luts
self.choosenlut = selected_lut
y_count = len(self.metadata)
self.table = QtGui.QTableWidget(y_count,6)
self.table.setHorizontalHeaderLabels(['Clip', 'Videocodec', 'FPS', 'Audiocodec', 'Start Timecode', 'LUT'])
x = y = 0
for items in self.metadata:
for entry in items:
#print entry
self.table.setItem(y, x, QtGui.QTableWidgetItem(entry))
self.table.resizeColumnToContents(x)
x += 1
self.comb = QtGui.QComboBox()
for lutname in self.curentluts:
self.comb.addItem(lutname)
self.comb.setCurrentIndex(self.choosenlut)
self.table.setCellWidget(y, 5, self.comb)
self.table.setColumnWidth(5, 230)
y += 1
x = 0
self.table.resizeRowsToContents()
layout = QtGui.QHBoxLayout()
layout.addWidget(self.table)
self.setLayout(layout)
self.show()
def closeEvent(self, event): #check if window was closed
print "Cliplist Window was closed! "
答案 0 :(得分:0)
我检查你的代码已经完成,好的,让我解释一下。
AttributeError: 'Ui_MainWindow' object has no attribute 'openTable'
此错误说,“我在类Ui_MainWindow中没有self.openTable(这是因为你已创建自己的方法)”。
为什么?:因为问题与此相符;
def showCliplist(self):
data = self.metadata_list
luts = self.lutlist
selected_lut = self.LUTBox.currentIndex()
openTable = ClipListViewer(data,luts,selected_lut) # <- (1) THIS LINE
self.openTable.show() # <- (2) THIS LINE
在(1)中,您将第二个小部件创建到openTable
(不是self.openTable
)。
我们在openTable
(非self.openTable
)中有此对象。
然后(2),您的通话QtGui.QWidget.show(self)
显示self.openTable
(非openTable
)的小部件。它应该是错误的,因为我们没有变量self.openTable
。要修复它,你应该使用相同的名称变量,像这样;
def showCliplist(self):
data = self.metadata_list
luts = self.lutlist
selected_lut = self.LUTBox.currentIndex()
self.openTable = ClipListViewer(data,luts,selected_lut) # <- FIX THIS LINE !
self.openTable.show() # <- (2) THIS LINE
为什么在我致电self.openTable.show()
之前显示第二个小部件?
回答:您可以在第二个小部件中看到在最后一行代码中启动,您在启动结束前看到QtGui.QWidget.show(self)
已被调用;
class ClipListViewer(QtGui.QWidget):
def __init__(self, data, luts, selected_lut, parent = None):
super(ClipListViewer,self).__init__()
.
.
.
self.show() # <- THIS LINE
此致