我有一個主要的PyQt窗口,從中我需要得到一個用戶輸入的字符串,當他們擊中某個按鈕。從主PyQt窗口啓動一個PyQT窗口,並獲得用戶輸入?
這裏是我的用戶輸入窗口代碼:
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)
從主窗口中,我說:
inputter = InputDialog(mainWindowUI, title="comments", label="comments", text="")
inputter.show()
comment = inputter.text.text()
print comment
這將打印一個空字符串,即使用戶類型評論並點擊「確定」。很明顯,因爲主窗口腳本不會在InputDialog
上等待關閉。那麼,如何讓它等待,以便我可以檢索用戶輸入?
這很有效!輝煌,謝謝! –