前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python pyqt5 弹出框传递数据

python pyqt5 弹出框传递数据

作者头像
用户5760343
发布2019-07-22 11:17:47
1.8K0
发布2019-07-22 11:17:47
举报
文章被收录于专栏:sktj

-- coding: utf-8 --

''' 【简介】

代码语言:javascript
复制
对话框关闭时返回值给主窗口 例子

'''

from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * import sys

class DateDialog(QDialog): def init(self, parent=None): super(DateDialog, self).init(parent) self.setWindowTitle('DateDialog')

代码语言:javascript
复制
    # 在布局中添加部件
    layout = QVBoxLayout(self)
    self.datetime = QDateTimeEdit(self)
    self.datetime.setCalendarPopup(True)
    self.datetime.setDateTime(QDateTime.currentDateTime())
    layout.addWidget(self.datetime)

    # 使用两个button(ok和cancel)分别连接accept()和reject()槽函数
    buttons = QDialogButtonBox(
        QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
        Qt.Horizontal, self)
    buttons.accepted.connect(self.accept)
    buttons.rejected.connect(self.reject)
    layout.addWidget(buttons)

# 从对话框中获取当前日期和时间
def dateTime(self):
    return self.datetime.dateTime()

# 静态方法创建对话框并返回 (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)

-- coding: utf-8 --

''' 【简介】 对话框关闭时返回值给主窗口例子

'''

class WinForm(QWidget): def init(self, parent=None): super(WinForm, self).init(parent) self.resize(400, 90) self.setWindowTitle('对话框关闭时返回值给主窗口例子')

代码语言:javascript
复制
    self.lineEdit = QLineEdit(self)
    self.button1 = QPushButton('弹出对话框1')
    self.button1.clicked.connect(self.onButton1Click)

    self.button2 = QPushButton('弹出对话框2')
    self.button2.clicked.connect(self.onButton2Click)

    gridLayout = QGridLayout()
    gridLayout.addWidget(self.lineEdit)
    gridLayout.addWidget(self.button1)
    gridLayout.addWidget(self.button2)
    self.setLayout(gridLayout)

def onButton1Click(self):
    dialog = DateDialog(self)
    result = dialog.exec_()
    date = dialog.dateTime()
    self.lineEdit.setText(date.date().toString())
    print('\n日期对话框的返回值')
    print('date=%s' % str(date.date()))
    print('time=%s' % str(date.time()))
    print('result=%s' % result)
    dialog.destroy()

def onButton2Click(self):
    date, time, result = DateDialog.getDateTime()
    self.lineEdit.setText(date.toString())
    print('\n日期对话框的返回值')
    print('date=%s' % str(date))
    print('time=%s' % str(time))
    print('result=%s' % result)
    if result == QDialog.Accepted:
        print('点击确认按钮')
    else:
        print('点击取消按钮')

if name == "main": app = QApplication(sys.argv) form = WinForm() form.show() sys.exit(app.exec_())

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.07.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • -- coding: utf-8 --
  • -- coding: utf-8 --
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档