【发布时间】:2019-02-13 16:30:41
【问题描述】:
我完全是 Python 和 PyQT5 的菜鸟。还有我在 stackoverflow 上的第一篇文章。请原谅我可能缺少的任何协议。我正在创建一个读取 excel 文件的程序。此 XL 文件将有多个工作表。该程序首先创建一个主窗口,在其中收集一些信息。用户按下按钮后,程序会打开一个带有 QTabWidget 的新窗口。
这个 QTabWidget 是动态创建的,因此选项卡名称由 XL 工作表名称列表设置。在每个选项卡中,我想放入标签和组合框的 QFormLayout。每个选项卡的 qformlayout 都相同,但我想为每个选项卡上的每个组合框动态设置选择。
这是我的代码:
class TabWindow(QWidget):
def __init__(self):
super().__init__()
self.title = 'Tape Column Mapping'
self.left = 100
self.top = 100
self.width = 640
self.height = 570
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.lbl = QLabel('For each worksheet you want to analyze, please associate each field with the appropriate worksheet column:',self)
self.lbl.setGeometry(10,10,621,31)
self.lbl.setFont(QFont("MS Shell Dlg 2", 8, QFont.Bold))
def initFormLayout(self):
self.formLayout = QFormLayout(self)
self.formLayout.addRow(QLabel("Address"),QComboBox())
self.formLayout.addRow(QLabel("City"),QComboBox())
self.formLayout.addRow(QLabel("State"),QComboBox())
self.formLayout.addRow(QLabel("Zip"),QComboBox())
self.formLayout.addRow(QLabel("UPB"),QComboBox())
self.formLayout.addRow(QLabel("Interest Rate"),QComboBox())
self.formLayout.addRow(QLabel("P&I"),QComboBox())
self.formLayout.addRow(QLabel("Term"),QComboBox())
self.formLayout.addRow(QLabel("Original Balance"),QComboBox())
self.formLayout.addRow(QLabel("Note Date"),QComboBox())
self.formLayout.addRow(QLabel("Last Paid To"),QComboBox())
self.formLayout.addRow(QLabel("Next Due Date"),QComboBox())
self.formLayout.addRow(QLabel("Maturity Date"),QComboBox())
self.formLayout.addRow(QLabel("Asset Type"),QComboBox())
self.formLayout.addRow(QLabel("Note Status"),QComboBox())
self.setLayout(formLayout)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.title = 'NPN Tape Analyzer'
self.left = 100
self.top = 100
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.lbl = QLabel('NPN Tape Analyzer',self)
self.lbl.setGeometry(20,10,231,31)
self.lbl.setFont(QFont("MS Shell Dlg 2", 16, QFont.Bold))
self.lbl1 = QLabel('(Open Tape Excel File)',self)
self.lbl1.setGeometry(250,100,140,20)
self.lbl1.setFont(QFont("MS Shell Dlg 2", 8))
self.lbl1.setAlignment(Qt.AlignCenter)
self.lbl2 = QLabel('Select Worksheets to analyze (ctrl + click for multiple selections):',self)
self.lbl2.setGeometry(50,190,420,20)
self.lbl2.setFont(QFont("MS Shell Dlg 2", 9, QFont.Bold))
btn = QPushButton('START ANALYSIS',self)
btn.setGeometry(260,60,121,41)
btn.setFont(QFont("Arial", 8, QFont.Bold))
btn.clicked.connect(self.openXLworksheetDialog)
btn1 = QPushButton('NEXT >>',self)
btn1.setGeometry(500,280,75,23)
btn1.setFont(QFont("Arial", 8))
btn1.clicked.connect(self.collectSelectedSheets)
btn2 = QPushButton('Exit Application',self)
btn2.setGeometry(210,380,220,40)
btn2.setFont(QFont("Arial", 10, QFont.Bold))
btn2.clicked.connect(self.close)
self.fnametextBox = QLineEdit(self)
self.fnametextBox.setGeometry(35,150,571,20)
self.fnametextBox.setText("<No File>")
self.fnametextBox.setAlignment(Qt.AlignCenter)
self.fnametextBox.setStyleSheet("color: rgb(128, 128, 128);")
self.fnametextBox.setReadOnly(True)
self.sheetListWidg = QListWidget(self)
self.sheetListWidg.setGeometry(200,220,241,151)
self.sheetListWidg.setStyleSheet("color: rgb(0, 0, 255);")
self.sheetListWidg.setSelectionMode(QAbstractItemView.ExtendedSelection)
def openXLworksheetDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
XLfilename, _ = QFileDialog.getOpenFileName(self,"Open MS Excel file","","Newer Excel files (*.xlsx);;Older Excel files (*.xls)",options=options)
if XLfilename:
self.fnametextBox.setText(XLfilename)
self.WBdict = pd.read_excel(XLfilename, sheet_name=None)
for key in self.WBdict:
self.sheetListWidg.addItem(key)
def collectSelectedSheets(self):
tmpselSheets = self.sheetListWidg.selectedItems()
self.selSheetList = []
for i in range(len(tmpselSheets)):
self.selSheetList.append(str(self.sheetListWidg.selectedItems()[i].text()))
print(self.selSheetList)
def closeEvent(self, event):
"""Generate 'question' dialog on clicking 'X' button in title bar.
Reimplement the closeEvent() event handler to include a 'Question'
dialog with options on how to proceed - Close or Cancel buttons
"""
reply = QMessageBox.question(
self, "Message",
"You are exiting the application. Are you sure you want to quit?",
QMessageBox.Close | QMessageBox.Cancel,
QMessageBox.Close)
if reply == QMessageBox.Close:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
但我不认为 TabWindow 类部分是正确的,尤其是“self”的使用。我还没有尝试实例化 TabWindow 部分。但到目前为止,MainWindow 部分仍然有效。以下是我的问题:
1) 如果这是第二个窗口,我应该在第二个窗口类中使用 self 吗?
2) 在 TabWidget 中创建 QComboBoxes 后,我将如何访问它们?某种列表?
【问题讨论】: