关闭后,如何显示PyQt模式对话框并从控件中获取数据?
发布于 2021-01-29 15:08:44
对于像QInputDialog这样的内置对话框,我读到可以做到这一点:
text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
如何使用在Qt Designer中设计的对话框来模拟这种行为?例如,我想做:
my_date, my_time, ok = MyCustomDateTimeDialog.get_date_time(self)
关注者
0
被浏览
99
1 个回答
-
这是您可以用来提示输入日期的简单类:
class DateDialog(QDialog): def __init__(self, parent = None): super(DateDialog, self).__init__(parent) layout = QVBoxLayout(self) # nice widget for editing the date self.datetime = QDateTimeEdit(self) self.datetime.setCalendarPopup(True) self.datetime.setDateTime(QDateTime.currentDateTime()) layout.addWidget(self.datetime) # OK and Cancel buttons buttons = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) buttons.accepted.connect(self.accept) buttons.rejected.connect(self.reject) layout.addWidget(buttons) # get current date and time from the dialog def dateTime(self): return self.datetime.dateTime() # static method to create the dialog and return (date, time, accepted) @staticmethod def getDateTime(parent = None): dialog = DateDialog(parent) result = dialog.exec_() date = dialog.dateTime() return (date.date(), date.time(), result == QDialog.Accepted)
并使用它:
date, time, ok = DateDialog.getDateTime()