【发布时间】:2021-04-07 15:11:19
【问题描述】:
我创建了一个应用程序,并在其中一个 Windows (CreateProjectWindow(QDialog)) 上填写了一份表格,然后我使用 self.buttonBox.accepted.connect(self.getInfo) 提交了该表格。
提交时,我希望UpdateProjecWindow(QDialog) 中的self.tableComboBox 自动并立即更新,而无需我先重新启动应用程序。 CreateProjectWindow(QDialog) 中的最后四行代码是我尝试过的,但它们都不起作用。下面是sn-p的代码:
from __future__ import print_function
from datetime import date
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import os
import subprocess
import pandas as pd
import time
Class CreateProjectWindow(QDialog):
def __init__(self):
super(CreateProjectWindow, self).__init__()
self.setWindowTitle("Create a new project")
self.setGeometry(100, 100, 300, 400)
self.timeStampLineEdit = QDateTime.currentDateTime().toString('MM-dd-yyyy hh:mm:ss')
self.surnameLineEdit = QLineEdit()
self.firstnameLineEdit = QLineEdit()
self.dateOfBirthLineEdit = QDateEdit(calendarPopup=True, displayFormat='MM-dd-yyyy')
self.createForm()
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.buttonBox.accepted.connect(self.getInfo)
self.buttonBox.rejected.connect(self.reject)
def getInfo():
output = {'timestamp': self.timeStampLineEdit,\
'surname' : self.surnameLineEdit,\
'firstname' : self.firstnameLineEdit,\
'dob' : self.dateOfBirthLineEdit}
df = pd.DataFrame(output, index=[0])
df.to_excel('to/some/local/path/profiles_data.xlsx')
return None
QApplication.processEvents() # attempt 1 but doesn't work
QApplication(sys.argv).reload() # attempt 2 but doesn't work
subprocess.Popen([sys.executable, FILEPATH]) # attempt 3 restarts the app which I don't want
os.execl(sys.executable, sys.executable, * sys.argv) # attempt 4 restarts the app which I don't want.
def creatForm(self):
layout = QFormLayout()
layout.addRow(QLabel("Surname"), self.surnameLineEdit)
layout.addRow(QLabel("First Name"), self.firstnameLineEdit)
layout.addRow(QLabel("D.O.B"), self.dateOfBirthLineEdit)
self.formGroupBox.setLayout(layout)
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.formGroupBox)
mainLayout.addWidget(self.buttonBox)
self.setLayout(mainLayout)
class UpdateProjectWindow(QDialog):
def __init__(self):
super(UpdateProjectWindow, self).__init__()
self.setWindowTitle("Parameter Inputs")
self.setGeometry(100, 100, 300, 400)
self.formGroupBox = QGroupBox("Some Window")
self.tableComboBox = QComboBox()
df = pd.read_excel('to/some/local/path/profiles_data.xlsx')
names = df['surname']
self.tableComboBox.addItems(names)
createForm()
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.buttonBox.accepted.connect(self.processInfo)
self.buttonBox.rejected.connect(self.reject)
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.formGroupBox)
mainLayout.addWidget(self.buttonBox)
self.setLayout(mainLayout)
def processInfo(self):
get_name = self.tableComboBox.currentText()
print(get_name)
def createForm(self):
layout = QFormLayout()
layout.addRow(QLabel("Select Surname"), self.tableComboBox)
self.formGroupBox.setLayout(layout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# setting window title
self.setWindowTitle("MyApp")
# setting geometry to the window
self.setGeometry(200, 200, 400, 10)
self.window1 = CreateProjectWindow()
self.window2 = UpdateProjectWindow()
l = QVBoxLayout()
button1 = QPushButton("Create a Project")
button1.clicked.connect(
lambda checked: self.toggle_window(self.window1)
)
l.addWidget(button1)
button2 = QPushButton("Update a Project")
button2.clicked.connect(
lambda checked: self.toggle_window(self.window2)
)
l.addWidget(button2)
w = QWidget()
w.setLayout(l)
self.setCentralWidget(w)
def toggle_window(self, window):
if window.isVisible():
window.hide()
else:
window.show()
if __name__=="__main__":
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
【问题讨论】:
-
附代码时请注意:您的示例包含多个问题(“Class”应该是小写的,缩进问题很多,对
createForm的函数调用都是错误的,并且有属性在声明之前使用)。在粘贴之前总是尝试你的代码,我们应该专注于你的代码做了什么,而不是纠正你的语法。
标签: python-3.x pandas pyqt5