本文介紹了如何顯示 PyQt 模式對話框并在關閉后從其控件中獲取數據?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
對于像 QInputDialog 這樣的內置對話框,我讀到我可以這樣做:
For a built-in dialog like QInputDialog, I've read that I can do this:
text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
如何使用我在 Qt Designer 中自己設計的對話框來模擬這種行為?例如,我想做:
How can I emulate this behavior using a dialog that I design myself in Qt Designer? For instance, I would like to do:
my_date, my_time, ok = MyCustomDateTimeDialog.get_date_time(self)
推薦答案
這是一個簡單的類,你可以用它來提示日期:
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)
并使用它:
date, time, ok = DateDialog.getDateTime()
這篇關于如何顯示 PyQt 模式對話框并在關閉后從其控件中獲取數據?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!