I have a main PyQt window, from which I need to get a string of User Input when they hit a certain button.
Here is my code for the User Input window:
class InputDialog(QtGui.QDialog):
'''
this is for when you need to get some user input text
'''
def __init__(self, parent=None, title='user input', label='comment', text=''):
QtGui.QWidget.__init__(self, parent)
#--Layout Stuff---------------------------#
mainLayout = QtGui.QVBoxLayout()
layout = QtGui.QHBoxLayout()
self.label = QtGui.QLabel()
self.label.setText(label)
layout.addWidget(self.label)
self.text = QtGui.QLineEdit(text)
layout.addWidget(self.text)
mainLayout.addLayout(layout)
#--The Button------------------------------#
layout = QtGui.QHBoxLayout()
button = QtGui.QPushButton("okay") #string or icon
self.connect(button, QtCore.SIGNAL("clicked()"), self.close)
layout.addWidget(button)
mainLayout.addLayout(layout)
self.setLayout(mainLayout)
self.resize(400, 60)
self.setWindowTitle(title)
From the main window, I am saying:
inputter = InputDialog(mainWindowUI, title="comments", label="comments", text="")
inputter.show()
comment = inputter.text.text()
print comment
This prints an empty string, even if the user types a comment and hits “OK”. Obviously because the main window script does not wait on the InputDialog to close. So, how do I get it to wait, so that I may retrieve the user input?
Use
instead of
From: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qdialog.html#exec