gpt4 book ai didi

python - 如何显示 PyQt 模式对话框并在关闭后从其控件中获取数据?

转载 作者:IT老高 更新时间:2023-10-28 21:14:50 29 4
gpt4 key购买 nike

对于像 QInputDialog 这样的内置对话框,我读到我可以这样做:

text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')

如何使用我在 Qt Designer 中自己设计的对话框来模拟这种行为?例如,我想做:

my_date, my_time, ok = MyCustomDateTimeDialog.get_date_time(self)

最佳答案

这是一个简单的类,您可以使用它来提示日期:

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()

关于python - 如何显示 PyQt 模式对话框并在关闭后从其控件中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18196799/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com