python3+PyQt5+Qt Designer实现扩展对话框


Posted in Python onApril 20, 2018

本文是对《Python Qt GUI快速编程》的第9章的扩展对话框例子Find and replace用Python3+PyQt5+Qt Designer进行改写。

第一部分无借用Qt Designer,完全用代码实现。
第二部分则借用Qt Designer,快速实现。

第一部分:

import sys
from PyQt5.QtCore import Qt,pyqtSignal
from PyQt5.QtWidgets import (QApplication, QCheckBox, QDialog, QFrame,
  QGridLayout, QHBoxLayout, QLabel, QLayout, QLineEdit,
  QPushButton, QVBoxLayout)



class FindAndReplaceDlg(QDialog):
 find = pyqtSignal(str,bool,bool,bool,bool,bool)
 replace = pyqtSignal(str,str,bool,bool,bool,bool,bool)  

 def __init__(self, parent=None):
  super(FindAndReplaceDlg, self).__init__(parent)

  findLabel = QLabel("Find &what:")
  self.findLineEdit = QLineEdit()
  findLabel.setBuddy(self.findLineEdit)
  replaceLabel = QLabel("Replace w&ith:")
  self.replaceLineEdit = QLineEdit()
  replaceLabel.setBuddy(self.replaceLineEdit)
  self.caseCheckBox = QCheckBox("&Case sensitive")
  self.wholeCheckBox = QCheckBox("Wh&ole words")
  self.wholeCheckBox.setChecked(True)
  self.moreFrame = QFrame()
  self.moreFrame.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken)
  self.backwardsCheckBox = QCheckBox("Search &Backwards")
  self.regexCheckBox = QCheckBox("Regular E&xpression")
  self.ignoreNotesCheckBox = QCheckBox("Ignore foot¬es "
             "and endnotes")
  line = QFrame()
  line.setFrameStyle(QFrame.VLine|QFrame.Sunken)
  self.findButton = QPushButton("&Find")
  self.replaceButton = QPushButton("&Replace")
  closeButton = QPushButton("Close")
  self.moreButton = QPushButton("&More")
  self.moreButton.setCheckable(True)


  gridLayout = QGridLayout()
  gridLayout.addWidget(findLabel, 0, 0)
  gridLayout.addWidget(self.findLineEdit, 0, 1)
  gridLayout.addWidget(replaceLabel, 1, 0)
  gridLayout.addWidget(self.replaceLineEdit, 1, 1)
  frameLayout = QVBoxLayout()
  frameLayout.addWidget(self.backwardsCheckBox)
  frameLayout.addWidget(self.regexCheckBox)
  frameLayout.addWidget(self.ignoreNotesCheckBox)
  self.moreFrame.setLayout(frameLayout)
  leftLayout = QVBoxLayout()
  leftLayout.addLayout(gridLayout)
  leftLayout.addWidget(self.caseCheckBox)
  leftLayout.addWidget(self.wholeCheckBox)
  leftLayout.addWidget(self.moreFrame)
  buttonLayout = QVBoxLayout()
  buttonLayout.addWidget(self.findButton)
  buttonLayout.addWidget(self.replaceButton)
  buttonLayout.addWidget(closeButton)
  buttonLayout.addWidget(self.moreButton)
  buttonLayout.addStretch()
  mainLayout = QHBoxLayout()
  mainLayout.addLayout(leftLayout)
  mainLayout.addWidget(line)
  mainLayout.addLayout(buttonLayout)
  self.setLayout(mainLayout)

  self.moreFrame.hide()
  mainLayout.setSizeConstraint(QLayout.SetFixedSize)

  self.moreButton.toggled[bool].connect(self.setvisible)


  self.findLineEdit.textEdited.connect(self.updateUi)
  self.findButton.clicked.connect(self.findClicked)
  self.replaceButton.clicked.connect(self.replaceClicked)

  self.updateUi()
  self.setWindowTitle("Find and Replace")

 def setvisible(self,YN):
  self.moreFrame.setVisible(YN)


 def findClicked(self):
  self.find.emit(self.findLineEdit.text(),
    self.caseCheckBox.isChecked(),
    self.wholeCheckBox.isChecked(),
    self.backwardsCheckBox.isChecked(),
    self.regexCheckBox.isChecked(),
    self.ignoreNotesCheckBox.isChecked())


 def replaceClicked(self):
  self.replace.emit(self.findLineEdit.text(),
    self.replaceLineEdit.text(),
    self.caseCheckBox.isChecked(),
    self.wholeCheckBox.isChecked(),
    self.backwardsCheckBox.isChecked(),
    self.regexCheckBox.isChecked(),
    self.ignoreNotesCheckBox.isChecked())


 def updateUi(self):
  enable = self.findLineEdit.text()
  self.findButton.setEnabled(bool(enable))
  self.replaceButton.setEnabled(bool(enable))


if __name__ == "__main__":

 def find(what, *args):
  print("Find {0} {1}".format(what, [x for x in args]))

 def replace(old, new, *args):
  print("Replace {0} with {1} {2}".format(
    old, new, [x for x in args]))

 app = QApplication(sys.argv)
 form = FindAndReplaceDlg()
 form.find.connect(find)
 form.replace.connect(replace)  
 form.show()
 app.exec_()

第二部分:

/home/yrd/eric_workspace/chap09/findandreplacedlg/Ui_findandreplacedlg.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '/home/yrd/eric_workspace/chap09/findandreplacedlg/findandreplacedlg.ui'
#
# Created by: PyQt5 UI code generator 5.7
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_FindAndReplaceDlg(object):
 def setupUi(self, FindAndReplaceDlg):
  FindAndReplaceDlg.setObjectName("FindAndReplaceDlg")
  FindAndReplaceDlg.resize(355, 274)
  self.mainlayout = QtWidgets.QHBoxLayout(FindAndReplaceDlg)
  self.mainlayout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize)
  self.mainlayout.setContentsMargins(9, 9, 9, 9)
  self.mainlayout.setSpacing(6)
  self.mainlayout.setObjectName("mainlayout")
  self.vboxlayout = QtWidgets.QVBoxLayout()
  self.vboxlayout.setContentsMargins(0, 0, 0, 0)
  self.vboxlayout.setSpacing(6)
  self.vboxlayout.setObjectName("vboxlayout")
  self.gridlayout = QtWidgets.QGridLayout()
  self.gridlayout.setContentsMargins(0, 0, 0, 0)
  self.gridlayout.setSpacing(6)
  self.gridlayout.setObjectName("gridlayout")
  self.replaceLineEdit = QtWidgets.QLineEdit(FindAndReplaceDlg)
  self.replaceLineEdit.setObjectName("replaceLineEdit")
  self.gridlayout.addWidget(self.replaceLineEdit, 1, 1, 1, 1)
  self.findLineEdit = QtWidgets.QLineEdit(FindAndReplaceDlg)
  self.findLineEdit.setObjectName("findLineEdit")
  self.gridlayout.addWidget(self.findLineEdit, 0, 1, 1, 1)
  self.label_2 = QtWidgets.QLabel(FindAndReplaceDlg)
  self.label_2.setObjectName("label_2")
  self.gridlayout.addWidget(self.label_2, 1, 0, 1, 1)
  self.label = QtWidgets.QLabel(FindAndReplaceDlg)
  self.label.setObjectName("label")
  self.gridlayout.addWidget(self.label, 0, 0, 1, 1)
  self.vboxlayout.addLayout(self.gridlayout)
  self.vboxlayout1 = QtWidgets.QVBoxLayout()
  self.vboxlayout1.setContentsMargins(0, 0, 0, 0)
  self.vboxlayout1.setSpacing(6)
  self.vboxlayout1.setObjectName("vboxlayout1")
  self.caseCheckBox = QtWidgets.QCheckBox(FindAndReplaceDlg)
  self.caseCheckBox.setObjectName("caseCheckBox")
  self.vboxlayout1.addWidget(self.caseCheckBox)
  self.wholeCheckBox = QtWidgets.QCheckBox(FindAndReplaceDlg)
  self.wholeCheckBox.setChecked(True)
  self.wholeCheckBox.setObjectName("wholeCheckBox")
  self.vboxlayout1.addWidget(self.wholeCheckBox)
  self.vboxlayout.addLayout(self.vboxlayout1)
  spacerItem = QtWidgets.QSpacerItem(231, 16, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
  self.vboxlayout.addItem(spacerItem)
  self.moreFrame = QtWidgets.QFrame(FindAndReplaceDlg)
  self.moreFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
  self.moreFrame.setFrameShadow(QtWidgets.QFrame.Raised)
  self.moreFrame.setObjectName("moreFrame")
  self.vboxlayout2 = QtWidgets.QVBoxLayout(self.moreFrame)
  self.vboxlayout2.setContentsMargins(9, 9, 9, 9)
  self.vboxlayout2.setSpacing(6)
  self.vboxlayout2.setObjectName("vboxlayout2")
  self.backwardsCheckBox = QtWidgets.QCheckBox(self.moreFrame)
  self.backwardsCheckBox.setObjectName("backwardsCheckBox")
  self.vboxlayout2.addWidget(self.backwardsCheckBox)
  self.regexCheckBox = QtWidgets.QCheckBox(self.moreFrame)
  self.regexCheckBox.setObjectName("regexCheckBox")
  self.vboxlayout2.addWidget(self.regexCheckBox)
  self.ignoreNotesCheckBox = QtWidgets.QCheckBox(self.moreFrame)
  self.ignoreNotesCheckBox.setObjectName("ignoreNotesCheckBox")
  self.vboxlayout2.addWidget(self.ignoreNotesCheckBox)
  self.vboxlayout.addWidget(self.moreFrame)
  self.mainlayout.addLayout(self.vboxlayout)
  self.line = QtWidgets.QFrame(FindAndReplaceDlg)
  self.line.setFrameShape(QtWidgets.QFrame.VLine)
  self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
  self.line.setObjectName("line")
  self.mainlayout.addWidget(self.line)
  self.vboxlayout3 = QtWidgets.QVBoxLayout()
  self.vboxlayout3.setContentsMargins(0, 0, 0, 0)
  self.vboxlayout3.setSpacing(6)
  self.vboxlayout3.setObjectName("vboxlayout3")
  self.findButton = QtWidgets.QPushButton(FindAndReplaceDlg)
  self.findButton.setFocusPolicy(QtCore.Qt.NoFocus)
  self.findButton.setObjectName("findButton")
  self.vboxlayout3.addWidget(self.findButton)
  self.replaceButton = QtWidgets.QPushButton(FindAndReplaceDlg)
  self.replaceButton.setFocusPolicy(QtCore.Qt.NoFocus)
  self.replaceButton.setObjectName("replaceButton")
  self.vboxlayout3.addWidget(self.replaceButton)
  self.closeButton = QtWidgets.QPushButton(FindAndReplaceDlg)
  self.closeButton.setFocusPolicy(QtCore.Qt.NoFocus)
  self.closeButton.setObjectName("closeButton")
  self.vboxlayout3.addWidget(self.closeButton)
  self.moreButton = QtWidgets.QPushButton(FindAndReplaceDlg)
  self.moreButton.setFocusPolicy(QtCore.Qt.NoFocus)
  self.moreButton.setCheckable(True)
  self.moreButton.setObjectName("moreButton")
  self.vboxlayout3.addWidget(self.moreButton)
  spacerItem1 = QtWidgets.QSpacerItem(21, 16, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
  self.vboxlayout3.addItem(spacerItem1)
  self.mainlayout.addLayout(self.vboxlayout3)
  self.label_2.setBuddy(self.replaceLineEdit)
  self.label.setBuddy(self.findLineEdit)

  self.retranslateUi(FindAndReplaceDlg)
  self.closeButton.clicked.connect(FindAndReplaceDlg.reject)
  self.moreButton.toggled['bool'].connect(self.moreFrame.setVisible)
  QtCore.QMetaObject.connectSlotsByName(FindAndReplaceDlg)
  FindAndReplaceDlg.setTabOrder(self.findLineEdit, self.replaceLineEdit)
  FindAndReplaceDlg.setTabOrder(self.replaceLineEdit, self.caseCheckBox)
  FindAndReplaceDlg.setTabOrder(self.caseCheckBox, self.wholeCheckBox)
  FindAndReplaceDlg.setTabOrder(self.wholeCheckBox, self.backwardsCheckBox)
  FindAndReplaceDlg.setTabOrder(self.backwardsCheckBox, self.regexCheckBox)
  FindAndReplaceDlg.setTabOrder(self.regexCheckBox, self.ignoreNotesCheckBox)

 def retranslateUi(self, FindAndReplaceDlg):
  _translate = QtCore.QCoreApplication.translate
  FindAndReplaceDlg.setWindowTitle(_translate("FindAndReplaceDlg", "Find and Replace"))
  self.label_2.setText(_translate("FindAndReplaceDlg", "Replace w&ith:"))
  self.label.setText(_translate("FindAndReplaceDlg", "Find &what:"))
  self.caseCheckBox.setText(_translate("FindAndReplaceDlg", "&Case sensitive"))
  self.wholeCheckBox.setText(_translate("FindAndReplaceDlg", "Wh&ole words"))
  self.backwardsCheckBox.setText(_translate("FindAndReplaceDlg", "Search &Backwards"))
  self.regexCheckBox.setText(_translate("FindAndReplaceDlg", "Regular E&xpression"))
  self.ignoreNotesCheckBox.setText(_translate("FindAndReplaceDlg", "Ignore foot¬es and endnotes"))
  self.findButton.setText(_translate("FindAndReplaceDlg", "&Find"))
  self.replaceButton.setText(_translate("FindAndReplaceDlg", "&Replace"))
  self.closeButton.setText(_translate("FindAndReplaceDlg", "Close"))
  self.moreButton.setText(_translate("FindAndReplaceDlg", "&More"))

/home/yrd/eric_workspace/chap09/findandreplacedlg/findandreplacedlg.py

# -*- coding: utf-8 -*-

"""
Module implementing FindAndReplaceDlg.
"""

from PyQt5.QtCore import pyqtSlot,pyqtSignal
from PyQt5.QtWidgets import QDialog,QApplication

from Ui_findandreplacedlg import Ui_FindAndReplaceDlg


class FindAndReplaceDlg(QDialog, Ui_FindAndReplaceDlg):
 """
 Class documentation goes here.
 """
 find = pyqtSignal(str,bool,bool,bool,bool,bool)
 replace = pyqtSignal(str,str,bool,bool,bool,bool,bool)  
 def __init__(self, parent=None):
  """
  Constructor

  @param parent reference to the parent widget
  @type QWidget
  """
  super(FindAndReplaceDlg, self).__init__(parent)
  self.setupUi(self)
  self.moreFrame.hide()
  #self.layout().setSizeConstraint(QLayout.SetFixedSize)
  self.updateUi()  

 @pyqtSlot(str)
 def on_findLineEdit_textEdited(self, text):
  """
  Slot documentation goes here.

  @param p0 DESCRIPTION
  @type str
  """
  # TODO: not implemented yet
  self.updateUi()

 @pyqtSlot()
 def on_findButton_clicked(self):
  self.find.emit(self.findLineEdit.text(),
      self.caseCheckBox.isChecked(),
      self.wholeCheckBox.isChecked(),
      self.backwardsCheckBox.isChecked(),
      self.regexCheckBox.isChecked(),
      self.ignoreNotesCheckBox.isChecked())  


 @pyqtSlot()
 def on_replaceButton_clicked(self):
  self.replace.emit(self.findLineEdit.text(),
       self.replaceLineEdit.text(),
       self.caseCheckBox.isChecked(),
       self.wholeCheckBox.isChecked(),
       self.backwardsCheckBox.isChecked(),
       self.regexCheckBox.isChecked(),
       self.ignoreNotesCheckBox.isChecked())

 def updateUi(self):
  enable = self.findLineEdit.text()
  self.findButton.setEnabled(bool(enable))
  self.replaceButton.setEnabled(bool(enable))

if __name__ == "__main__":
 import sys

 def find(what, *args):
  print("Find {0} {1}".format(what, [x for x in args]))

 def replace(old, new, *args):
  print("Replace {0} with {1} {2}".format(
    old, new, [x for x in args]))

 app = QApplication(sys.argv)
 form = FindAndReplaceDlg()
 form.find.connect(find)
 form.replace.connect(replace)
 form.show()
 app.exec_()

运行结果:

python3+PyQt5+Qt Designer实现扩展对话框

python3+PyQt5+Qt Designer实现扩展对话框

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python操作MySQL简单实现方法
Jan 26 Python
Python常用的内置序列结构(列表、元组、字典)学习笔记
Jul 08 Python
Python文件和流(实例讲解)
Sep 12 Python
python和ruby,我选谁?
Sep 13 Python
1 行 Python 代码快速实现 FTP 服务器
Jan 25 Python
基于Django用户认证系统详解
Feb 21 Python
python读取目录下最新的文件夹方法
Dec 24 Python
python2.7使用plotly绘制本地散点图和折线图
Apr 02 Python
解决Python3用PIL的ImageFont输出中文乱码的问题
Aug 22 Python
python解析多层json操作示例
Dec 30 Python
python标识符命名规范原理解析
Jan 10 Python
python小程序基于Jupyter实现天气查询的方法
Mar 27 Python
pandas获取groupby分组里最大值所在的行方法
Apr 20 #Python
pandas多级分组实现排序的方法
Apr 20 #Python
Python PyQt4实现QQ抽屉效果
Apr 20 #Python
Python在groupby分组后提取指定位置记录方法
Apr 20 #Python
PyQt实现界面翻转切换效果
Apr 20 #Python
python3+PyQt5+Qt Designer实现堆叠窗口部件
Apr 20 #Python
python3 pandas 读取MySQL数据和插入的实例
Apr 20 #Python
You might like
php生成的html meta和link标记在body标签里 顶部有个空行
2010/05/18 PHP
PHP计算2点经纬度之间的距离代码
2013/08/12 PHP
详解yii2实现分库分表的方案与思路
2017/02/03 PHP
URL编码转换,escape() encodeURI() encodeURIComponent()
2006/12/27 Javascript
js对数字的格式化使用说明
2011/01/12 Javascript
jquery限制输入字数,并提示剩余字数实现代码
2012/12/24 Javascript
使用documentElement正确取得当前可见区域的大小
2014/07/25 Javascript
javascript中eval和with用法实例总结
2015/11/30 Javascript
有关jQuery中parent()和siblings()的小问题
2016/06/01 Javascript
jquery ui sortable拖拽后保存位置
2017/04/27 jQuery
详解NODEJS的http实现
2018/01/04 NodeJs
使用pkg打包Node.js应用的方法步骤
2018/10/19 Javascript
React组件对子组件children进行加强的方法
2019/06/23 Javascript
vant 时间选择器--开始时间和结束时间实例
2020/11/04 Javascript
Python中的模块和包概念介绍
2015/04/13 Python
Python 爬虫爬取指定博客的所有文章
2016/02/17 Python
深入浅析Python中join 和 split详解(推荐)
2016/06/30 Python
Python进行数据提取的方法总结
2016/08/22 Python
关于Python正则表达式 findall函数问题详解
2018/03/22 Python
Python处理CSV与List的转换方法
2018/04/19 Python
新手如何发布Python项目开源包过程详解
2019/07/11 Python
使用 Django Highcharts 实现数据可视化过程解析
2019/07/31 Python
Python多线程模块Threading用法示例小结
2019/11/09 Python
Python中Flask-RESTful编写API接口(小白入门)
2019/12/11 Python
Python 带星号(* 或 **)的函数参数详解
2021/02/23 Python
css3动画事件—webkitAnimationEnd与计时器time事件
2013/01/31 HTML / CSS
SmartBuyGlasses中国:唯视良品(销售名牌太阳镜、墨镜和眼镜框)
2017/07/03 全球购物
Tessabit日本:集世界奢侈品和设计师品牌的意大利精品买手店
2020/01/07 全球购物
香港士多网上超级市场:Ztore
2021/01/09 全球购物
群众路线教育实践活动方案
2014/02/02 职场文书
幼儿园父亲节活动方案
2014/03/11 职场文书
2016大学优秀学生干部事迹材料
2016/03/01 职场文书
深度好文:50条没人告诉你的人生经验,句句精辟
2019/08/22 职场文书
导游词之无锡梅园
2019/11/28 职场文书
为了顺利买到演唱会的票用Python制作了自动抢票的脚本
2021/10/16 Python
Hive常用日期格式转换语法
2022/06/25 数据库