pyqt 取消后退出程序 打开文件对话框_关闭后,如何显示PyQt模态对话框并从控件中获取数据?...

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:')

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)

and to use it:

date, time, ok = DateDialog.getDateTime()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值