Python 如何显示 PyQt 模态对话框并在其关闭后从其控件中获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18196799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I show a PyQt modal dialog and get data out of its controls once its closed?
提问by Thomas Johnson
For a built-in dialog like QInputDialog, I've read that I can do this:
对于像 QInputDialog 这样的内置对话框,我读到我可以这样做:
text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
How can I emulate this behavior using a dialog that I design myself in Qt Designer? For instance, I would like to do:
如何使用我在 Qt Designer 中自己设计的对话框来模拟这种行为?例如,我想做:
my_date, my_time, ok = MyCustomDateTimeDialog.get_date_time(self)
采纳答案by hluk
Here is simple class you can use to prompt for date:
这是您可以用来提示日期的简单类:
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)
and to use it:
并使用它:
date, time, ok = DateDialog.getDateTime()
回答by lou
I tried to edit the answer of hlukwith the changes below but it got rejected, not sure why because it got some clear bugs as far is I can see.
我试图用下面的更改来编辑hluk的答案,但它被拒绝了,不知道为什么,因为据我所知,它有一些明显的错误。
bugfix 1: removed self.from self.layout.addWidget(self.buttons)
错误修正 1:删除自我。来自self.layout.addWidget(self.buttons)
bugfix 2: connected OK and Cancel buttons to its correct actions
错误修正 2:将确定和取消按钮连接到正确的操作
enhancement: made the code ready to run by including the imports and improved the run example
增强功能:通过包含导入和改进运行示例使代码准备好运行
from PyQt4.QtGui import QDialog, QVBoxLayout, QDialogButtonBox, QDateTimeEdit, QApplication
from PyQt4.QtCore import Qt, QDateTime
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
self.buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
Qt.Horizontal, self)
layout.addWidget(self.buttons)
self.buttons.accepted.connect(self.accept)
self.buttons.rejected.connect(self.reject)
# 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)
and to use it:
并使用它:
app = QApplication([])
date, time, ok = DateDialog.getDateTime()
print("{} {} {}".format(date, time, ok))
app.exec_()