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

本文是对《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&notes "
                          "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&notes 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实现自定义窗口部件Counters
  • python3+PyQt5实现支持多线程的页面索引器应用程序
  • python3+PyQt5+Qt Designer实现堆叠窗口部件
  • python3+PyQt5重新实现QT事件处理程序
  • python3+PyQt5重新实现自定义数据拖放处理
  • python3使用pyqt5制作一个超简单浏览器的实例
  • Python3使用PyQt5制作简单的画板/手写板实例
  • python3.5 + PyQt5 +Eric6 实现的一个计算器代码
  • python3+PyQt5实现使用剪贴板做复制与粘帖示例
  • python3+PyQt5图形项的自定义和交互 python3实现page Designer应用程序
(0)

相关推荐

  • python3+PyQt5实现自定义窗口部件Counters

    本文通过Python3+PyQt5实现自定义部件–Counters自定 窗口部件.这个窗口是3*3的网格.本文有两个例子如下: /home/yrd/eric_workspace/chap11/counters.py. /home/yrd/eric_workspace/chap11/counters_dnd.py 第二个例子在第一个例子的基础上实现能通过鼠标拖拽球到不同的网格中. /home/yrd/eric_workspace/chap11/counters.py #!/usr/bin/env

  • python3.5 + PyQt5 +Eric6 实现的一个计算器代码

    目前可以实现简单的计算.计算前请重置,设计的时候默认数字是0,学了半天就做出来个这么个结果,bug不少. python3.5 + PyQt5 +Eric6 在windows7 32位系统可以完美运行 计算器,简单学了半天就画个图实现的存在bug,部分按钮还未实现,后续优化. 代码结构如图: jisuan.py import re #匹配整数或小数的乘除法,包括了开头存在减号的情况 mul_div=re.compile("(-?\d+)(\.\d+)?(\*|/)(-?\d+)(\.\d+)?&q

  • python3+PyQt5实现支持多线程的页面索引器应用程序

    本文通过Python3+pyqt5实现了python Qt GUI 快速编程的19章的页面索引器应用程序例子. /home/yrd/eric_workspace/chap19/walker_ans.py #!/usr/bin/env python3 import codecs import html.entities import re import sys from PyQt5.QtCore import (QMutex, QThread,pyqtSignal,Qt) class Walker

  • python3+PyQt5图形项的自定义和交互 python3实现page Designer应用程序

    本文通过Python3+PyQt5实现<python Qt Gui 快速编程>这本书的page Designer应用程序,采用QGraphicsView,QGraphicsScene,QGraphicsItem,这个程序包含有多个文本,图片和框的页面.有些图形类在PyQt5已过时,所以本代码改动幅度比较大.主要的类或方法的改变如下: QMatrix==>QTransform setMatrix==>setTransform rotate ==> setRotation 本例中

  • Python3使用PyQt5制作简单的画板/手写板实例

    1.前言 版本:Python3.6.1 + PyQt5 写一个程序的时候需要用到画板/手写板,只需要最简单的那种.原以为网上到处都是,结果找了好几天,都没有找到想要的结果. 网上的要么是非python版的qt程序(要知道qt版本之间差异巨大,还是非同一语言的),改写难度太大.要么是PyQt4的老程序,很多都已经不能在PyQt5上运行了.要么是大神写的特别复杂的程序,简直是直接做出了一个Windows自带的画图版,只能膜拜~ 于是我只能在众多代码中慢慢寻找自己需要的那一小部分,然后不断地拼凑,不断

  • python3+PyQt5实现使用剪贴板做复制与粘帖示例

    本文是对<Python Qt GUI快速编程>的第10章的例子剪贴板用Python3+PyQt5进行改写,分别对文本,图片和html文本的复制与粘帖,三种做法大同小异. #!/usr/bin/env python3 import os import sys from PyQt5.QtCore import (QMimeData, Qt) from PyQt5.QtWidgets import (QApplication, QDialog, QGridLayout, QLabel, QPushB

  • python3使用pyqt5制作一个超简单浏览器的实例

    我们使用的是QWebview模块,这里也主要是展示下QWebview的用法. 之前在网上找了半天的解析网页的内容,都不是很清楚. 这是核心代码: webview = Qwebview() webview.load(Qurl('http://www.cnblogs.com/Blaxon/')) webview.show() 完整代码(代码是拿的别的代码改得): from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.Q

  • python3+PyQt5+Qt Designer实现堆叠窗口部件

    本文是对<Python Qt GUI快速编程>的第9章的堆叠窗口例子Vehicle Rental用Python3+PyQt5+Qt Designer进行改写. 第一部分无借用Qt Designer,完全用代码实现. 第二部分则借用Qt Designer,快速实现. 第一部分: import sys from PyQt5.QtCore import (Qt) from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog, QDialo

  • python3+PyQt5重新实现自定义数据拖放处理

    本文分成两部分,第一部分通过python3+PyQt5实现自定义数据的拖放操作.第二部分则对第一部分的程序进行修改,增加拖放操作时,菜单提示是否移动或拷贝,还有可以通过ctrl键盘来设置移动过程中拷贝源而非会将源删除. 自定义数据MIME数据类型QMimeData,MIME是一种用于处理具有多个组成部分的自定义数据的标准化格式.MIME数据由一个数据类型和一个子类型构成–例如,text/plain,text/html,image/png,要处理自定义MIME数据,就必须要选用一种自定义数据类型和

  • python3+PyQt5重新实现QT事件处理程序

    本文是对<Python Qt GUI快速编程>的第10章的例子events用Python3+PyQt5进行改写,涉及到重新实现QWidget的事件处理程序.本例子涉及到上下文菜单,鼠标事件,键盘事件,可作为重新实现事件处理程序的参考. 注:在创建上下文菜单最简单的方式使用Qwidget.addAction()把动作添加到窗口部件中,再把窗口部件的上下文菜单策略设置为Qt.ActionsContextMenu即可,但是如果像本例子一样要根据不同的状态来提供不同的选项,则要重新实现上下文菜单事件处

随机推荐