当我在lineEdit小部件中输入值时(例如'insert_subject'),有人知道为什么我的GUI崩溃,然后按'Enter'然后用鼠标点击另一个字段。当我用鼠标点击时,程序运行正常。我怀疑它与我正在解释的editingFinished信号有关。
在此GUI中,我首先要求用户选择数据文件。然后我使用数据文件中的列名来设置lineEdit小部件的自动完成选项('insert_subject'和'depend_var')。我还根据已经选择的名称更新选项。
我是新手,因此欢迎任何代码改进建议。非常感谢
import sys
from PyQt4 import QtCore, QtGui
from trim_gui3 import Ui_trimWindow
import pandas as pd
import numpy as np
class StartQT4(QtGui.QMainWindow):
var_namesSet = QtCore.pyqtSignal() # create a custom signal to inform that the dataframe is available
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_trimWindow()
self.ui.setupUi(self)
self.ui.button_open.clicked.connect(self.file_dialog)
self.ui.button_trim.clicked.connect(self.trim)
# set icon
self.setWindowIcon(QtGui.QIcon('trim_icon.jpg'))
# initiate the autocomplete
self.var_namesSet.connect(self.var_completer)
self.ui.insert_subject.editingFinished.connect(self.which_line_edit)
self.ui.insert_depend_var.editingFinished.connect(self.which_line_edit)
def which_line_edit(self): # this function determines which variable was entered in the gui and removes it from the autocomplete list
# it also returns it to the autocomplete list if the field is cleared
sender = self.sender()
print sender.text()
if hasattr(self, 'var_names'):
if sender.text() in self.var_names:
new_var_names = list(self.var_names)
new_var_names.remove(sender.text())
print 'variable removed from the list'
self.var_completer(new_var_names)
elif self.ui.insert_subject.text() == '':
print 'variable returned to the list'
self.var_completer(self.var_names)
def var_completer(self, *arg): # this function takes variable number of arguments
completer = QtGui.QCompleter()
self.ui.insert_subject.setCompleter(completer)
self.model_subject = QtGui.QStringListModel()
completer.setModel(self.model_subject)
self.ui.insert_depend_var.setCompleter(completer)
self.model_depend_var = QtGui.QStringListModel()
completer.setModel(self.model_depend_var)
if arg:
self.model_subject.setStringList(arg[0])
self.model_depend_var.setStringList(arg[0])
else:
self.model_subject.setStringList(self.var_names)
self.model_depend_var.setStringList(self.var_names)
def file_dialog(self):
fd = QtGui.QFileDialog(self)
self.filename = fd.getOpenFileName()
print self.filename
self.ui.input_filename.setText(self.filename) # write the name of the opened file in the gui
self.df = pd.read_csv(str(self.filename), na_values='.');
self.var_names = list(self.df.columns) #get column variables list
self.var_namesSet.emit()
def trim(self):
pass
######################################################
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
#to prevent ipython console to be killed when you close the gui
#every time you close your qt application, the object will be deleted in python.
#app.aboutToQuit.connect(app.deleteLater)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_()) #this returns an error when run from ipython
#app.exec_( #this returns an error when running from command line
以下是GUI的代码:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_trimWindow(object):
def setupUi(self, trimWindow):
trimWindow.setObjectName(_fromUtf8("trimWindow"))
trimWindow.resize(622, 571)
self.centralwidget = QtGui.QWidget(trimWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.gridLayout_2 = QtGui.QGridLayout()
self.gridLayout_2.setContentsMargins(-1, 0, -1, -1)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setObjectName(_fromUtf8("label"))
self.gridLayout_2.addWidget(self.label, 1, 0, 1, 1)
self.button_open = QtGui.QPushButton(self.centralwidget)
self.button_open.setObjectName(_fromUtf8("button_open"))
self.gridLayout_2.addWidget(self.button_open, 0, 2, 1, 1)
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.gridLayout_2.addWidget(self.label_2, 2, 0, 1, 1)
self.insert_independ_var = QtGui.QLineEdit(self.centralwidget)
self.insert_independ_var.setObjectName(_fromUtf8("insert_independ_var"))
self.gridLayout_2.addWidget(self.insert_independ_var, 2, 1, 1, 1)
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.gridLayout_2.addWidget(self.label_3, 3, 0, 1, 1)
self.insert_subject = QtGui.QLineEdit(self.centralwidget)
self.insert_subject.setObjectName(_fromUtf8("insert_subject"))
self.gridLayout_2.addWidget(self.insert_subject, 1, 1, 1, 1)
self.insert_depend_var = QtGui.QLineEdit(self.centralwidget)
self.insert_depend_var.setObjectName(_fromUtf8("insert_depend_var"))
self.gridLayout_2.addWidget(self.insert_depend_var, 3, 1, 1, 1)
self.statusBox = QtGui.QTextEdit(self.centralwidget)
self.statusBox.setObjectName(_fromUtf8("statusBox"))
self.gridLayout_2.addWidget(self.statusBox, 5, 0, 1, 3)
self.label_5 = QtGui.QLabel(self.centralwidget)
self.label_5.setMinimumSize(QtCore.QSize(118, 23))
self.label_5.setObjectName(_fromUtf8("label_5"))
self.gridLayout_2.addWidget(self.label_5, 0, 0, 1, 1)
self.input_filename = QtGui.QLineEdit(self.centralwidget)
self.input_filename.setObjectName(_fromUtf8("input_filename"))
self.gridLayout_2.addWidget(self.input_filename, 0, 1, 1, 1)
self.progressBar = QtGui.QProgressBar(self.centralwidget)
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName(_fromUtf8("progressBar"))
self.gridLayout_2.addWidget(self.progressBar, 4, 0, 1, 3)
self.button_trim = QtGui.QPushButton(self.centralwidget)
self.button_trim.setObjectName(_fromUtf8("button_trim"))
self.gridLayout_2.addWidget(self.button_trim, 6, 1, 1, 1)
self.gridLayout.addLayout(self.gridLayout_2, 0, 0, 1, 1)
trimWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(trimWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
trimWindow.setStatusBar(self.statusbar)
self.retranslateUi(trimWindow)
QtCore.QMetaObject.connectSlotsByName(trimWindow)
trimWindow.setTabOrder(self.input_filename, self.button_open)
trimWindow.setTabOrder(self.button_open, self.insert_subject)
trimWindow.setTabOrder(self.insert_subject, self.insert_independ_var)
trimWindow.setTabOrder(self.insert_independ_var, self.insert_depend_var)
trimWindow.setTabOrder(self.insert_depend_var, self.statusBox)
trimWindow.setTabOrder(self.statusBox, self.button_trim)
def retranslateUi(self, trimWindow):
trimWindow.setWindowTitle(_translate("trimWindow", "Trim", None))
self.label.setText(_translate("trimWindow", "Subject number variable", None))
self.button_open.setText(_translate("trimWindow", "Select", None))
self.label_2.setText(_translate("trimWindow", "Iindependent variable(s)", None))
self.label_3.setText(_translate("trimWindow", "Dependent variable", None))
self.statusBox.setHtml(_translate("trimWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Ready.</span></p></body></html>", None))
self.label_5.setText(_translate("trimWindow", "Data Filename", None))
self.button_trim.setText(_translate("trimWindow", "Trim", None))