博客
关于我
PyQt5实现点击按钮获取文本框的值
阅读量:329 次
发布时间:2019-03-04

本文共 1735 字,大约阅读时间需要 5 分钟。

PyQt5中文本框的使用

在PyQt5中,中文本框是与用户交互的重要组成部分,常用于获取或设置文本输入。本文将详细介绍如何使用PyQt5中的QLineEdit类来实现文本框的操作。

1. 导入必要的库

首先,我们需要导入PyQt5中常用的库:

import sysfrom PyQt5.QtWidgets import (QApplication, QWidget, QPushButton,                             QAction, QMessageBox)from PyQt5.QtGui import QIconfrom PyQt5.QtCore import pyqtSlot

2. 创建主窗口

接下来,我们创建一个主窗口,并设置基本属性:

class App(QWidget):    def __init__(self):        super().__init__()        self.title = 'PyQt5 文本框示例'        self.left = 10        self.top = 10        self.width = 320        self.height = 200        self.initUI()

3. 初始化界面

initUI方法中,我们设置窗口标题和布局:

def initUI(self):        self.setWindowTitle(self.title)        self.setGeometry(self.left, self.top, self.width, self.height)                # 创建文本框        self.textbox = QLineEdit(self)        self.textbox.move(20, 20)        self.textbox.resize(280, 40)                # 创建显示按钮        self.button = QPushButton('显示文本', self)        self.button.move(20, 80)                # 绑定按钮点击事件        self.button.clicked.connect(self.on_click)                self.show()

4. 处理按钮点击事件

当按钮被点击时,on_click方法会执行:

@pyqtSlot()    def on_click(self):        # 获取文本框中的内容        typed_text = self.textbox.text()                # 显示确认对话框        QMessageBox.question(self, "信息",                          '你输入了文本:' + typed_text,                          QMessageBox.Ok,                           QMessageBox.Ok)                # 清空文本框        self.textbox.setText('')

5. 运行应用程序

最后,我们在主函数中运行应用程序:

if __name__ == '__main__':    app = QApplication(sys.argv)    ex = App()    app.exit(app.exec_())

使用说明

  • 创建文本框:使用QLineEdit类创建一个文本输入框,并设置其位置和大小
  • 绑定按钮事件:将按钮的点击事件与获取文本内容的方法绑定
  • 显示提示对话框:使用QMessageBox显示用户输入的内容
  • 清空文本框:在按钮点击后,自动清空文本框中的内容
  • 这个示例展示了如何通过PyQt5简单地与用户进行文本输入和输出,适合用于基本的用户输入处理场景。

    转载地址:http://whgq.baihongyu.com/

    你可能感兴趣的文章
    notepad++最详情汇总
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>