PyQt5: chapter2-将文本从一行编辑小部件复制并粘贴到另一行编辑小部件

  1. 创建Dialog without Buttonsons模板窗口
  2. 脱出两个Line Edit部件分别置于窗口的上方和下方
  3. 拖出两个Push Button部件置于中部,其text属性为Copy,Paste
  4. 建立关联关系如下:
    发送者信号接收者
    pushButtonpressed()lineEditselectAll()
    pushButtonreleased()lineEditcopy()
    pushButton_2clicked()lineEdit_2paste()

     

  5. 保存为demoSignal1.ui文件
  6. 使用pyuic工具生成demoSignal1.py文件
  7. 创建calldemoSignal1.py文件,代码如下
import sys
from PyQt5.QtWidgets import QDialog,QApplication
from cookbook_200420.demoSignal1 import *

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui=Ui_Dialog()
        self.ui.setupUi(self)
        self.show()
if __name__=="__main__":
    app=QApplication(sys.argv)
    w=MyForm()
    w.show()
    sys.exit(app.exec())

小记:

  • QtCore:QtCore模块是所有基于QT的应用程序的基础。它包含最基本的类,如QCoreApplication、QObject等。这些类执行重要任务,如事件处理、实现信号和插槽机制、I/O操作、处理字符串等。该模块包括几个类,包括QFile、QDir、QIODevice、QTimer、QString、QDate和QTime。
  • QtGui:顾名思义,QtGui模块包含开发跨平台GUI应用程序所需的类。该模块包含GUI类,如QCheckBox、QComboBox、QDateTimeEdit、QLineEdit、QPushButton、qpanter、QPaintDevice、QApplication、QTextEdit和QTextDocument。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过继承QLineEdit类来实现自定义输入框,具体步骤如下: 1. 导入PyQt模块,包括QWidget和QLineEdit类。 ``` from PyQt5.QtWidgets import QWidget, QLineEdit ``` 2. 创建一个新类,继承自QLineEdit。 ``` class MyLineEdit(QLineEdit): def __init__(self, parent=None): super().__init__(parent) ``` 3. 在__init__函数中设置输入框的样式和属性,例如设置背景颜色、边框样式、字体颜色等。 ``` self.setStyleSheet("background-color: white; border: 1px solid gray; color: black;") ``` 4. 实现一些自定义的功能,例如添加清空按钮、限制输入类型等。 ``` # 添加清空按钮 self.clearButton = QPushButton(self) self.clearButton.setIcon(QIcon("clear.png")) self.clearButton.setCursor(Qt.PointingHandCursor) self.clearButton.setStyleSheet("background-color: transparent; border: none;") self.clearButton.setVisible(False) self.clearButton.clicked.connect(self.clear) self.textChanged.connect(self.updateClearButton) # 限制输入类型 self.setValidator(QIntValidator()) ``` 5. 添加一些信号和槽函数,例如输入框文本变化时自动更新清空按钮的可见性。 ``` def updateClearButton(self, text): if text: self.clearButton.setVisible(True) else: self.clearButton.setVisible(False) ``` 6. 将自定义输入框添加到父口中。 ``` myLineEdit = MyLineEdit(self) myLineEdit.setGeometry(10, 10, 200, 30) ``` 完整代码示例: ``` from PyQt5.QtWidgets import QWidget, QLineEdit, QPushButton, QIntValidator from PyQt5.QtGui import QIcon from PyQt5.QtCore import Qt class MyLineEdit(QLineEdit): def __init__(self, parent=None): super().__init__(parent) self.setStyleSheet("background-color: white; border: 1px solid gray; color: black;") # 添加清空按钮 self.clearButton = QPushButton(self) self.clearButton.setIcon(QIcon("clear.png")) self.clearButton.setCursor(Qt.PointingHandCursor) self.clearButton.setStyleSheet("background-color: transparent; border: none;") self.clearButton.setVisible(False) self.clearButton.clicked.connect(self.clear) self.textChanged.connect(self.updateClearButton) # 限制输入类型 self.setValidator(QIntValidator()) def updateClearButton(self, text): if text: self.clearButton.setVisible(True) else: self.clearButton.setVisible(False) def resizeEvent(self, event): # 设置清空按钮位置 buttonSize = 20 self.clearButton.setGeometry(self.width() - buttonSize - 2, (self.height() - buttonSize) / 2, buttonSize, buttonSize) super().resizeEvent(event) if __name__ == "__main__": import sys from PyQt5.QtWidgets import QApplication app = QApplication(sys.argv) widget = QWidget() widget.resize(300, 200) myLineEdit = MyLineEdit(widget) myLineEdit.setGeometry(10, 10, 200, 30) widget.show() sys.exit(app.exec_()) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值