【PyQt5-Qt Designer】文本框读写操作
主要内容:
1、读、写 输入控件(Input Widgets)中的内容(str)
2、保存数据到txt文件
3、从txt文件中读内容,与输入控件中内容比较

将上述各种输入控件(Input Widgets)中的内容保存到txt文件中:
# -*- coding: utf-8 -*- from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(839, 589)
Dialog.setSizeGripEnabled(True)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(210, 390, 91, 41))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(530, 390, 91, 41))
self.pushButton_2.setObjectName("pushButton_2")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(140, 460, 291, 20))
self.lineEdit.setObjectName("lineEdit")
self.textEdit = QtWidgets.QTextEdit(Dialog)
self.textEdit.setGeometry(QtCore.QRect(140, 110, 541, 261))
self.textEdit.setObjectName("textEdit")
self.plainTextEdit = QtWidgets.QPlainTextEdit(Dialog)
self.plainTextEdit.setGeometry(QtCore.QRect(140, 490, 441, 91))
self.plainTextEdit.setObjectName("plainTextEdit")
self.spinBox = QtWidgets.QSpinBox(Dialog)
self.spinBox.setGeometry(QtCore.QRect(30, 290, 81, 22))
self.spinBox.setObjectName("spinBox")
self.doubleSpinBox = QtWidgets.QDoubleSpinBox(Dialog)
self.doubleSpinBox.setGeometry(QtCore.QRect(30, 340, 81, 22))
self.doubleSpinBox.setProperty("showGroupSeparator", False)
self.doubleSpinBox.setPrefix("")
self.doubleSpinBox.setProperty("value", 3.14)
self.doubleSpinBox.setObjectName("doubleSpinBox")
self.comboBox = QtWidgets.QComboBox(Dialog)
self.comboBox.setGeometry(QtCore.QRect(30, 60, 141, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.fontComboBox = QtWidgets.QFontComboBox(Dialog)
self.fontComboBox.setGeometry(QtCore.QRect(230, 60, 189, 22))
self.fontComboBox.setObjectName("fontComboBox") self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "确定/保存"))
self.pushButton_2.setText(_translate("Dialog", "退出"))
self.lineEdit.setText(_translate("Dialog", ""))
self.textEdit.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">input content:</span></p></body></html>"))
self.plainTextEdit.setPlainText(_translate("Dialog", "plainTextEdit"))
self.comboBox.setItemText(0, _translate("Dialog", "item1"))
self.comboBox.setItemText(1, _translate("Dialog", "item2"))
self.comboBox.setItemText(2, _translate("Dialog", "item3"))
self.comboBox.setItemText(3, _translate("Dialog", "item4"))
self.comboBox.setItemText(4, _translate("Dialog", "item5"))
self.comboBox.setItemText(5, _translate("Dialog", "item6")) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
Ui文件
# -*- coding: utf-8 -*- """
Module implementing file_dailog.
"""
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QDialog
from PyQt5 import QtWidgets
from Ui_file_operation import Ui_Dialog class file_dailog(QDialog, Ui_Dialog):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
super(file_dailog, self).__init__(parent)
self.setupUi(self)
self.pushButton.mousePressEvent = self.pushButton_clicked def pushButton_clicked(self, a):
self.logging_data() @pyqtSlot()
def on_pushButton_2_clicked(self):
sys.exit(0) def logging_data(self):
with open(r'logs\data.txt', 'w+') as f:
f.write(self.textEdit.toPlainText()+'\n')
f.write(self.lineEdit.text()+'\n')
f.write(self.plainTextEdit.toPlainText()+'\n')
f.write(self.comboBox.currentText()+'\n')
f.write(self.fontComboBox.currentText()+'\n')
f.write(self.fontComboBox.currentText()+'\n')
f.write(str(self.spinBox.value())+'\n')
f.write(str(self.doubleSpinBox.value())+'\n') if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = file_dailog()
ui.show()
sys.exit(app.exec_())
Main文件
实战案例:
登录框--->输入账号密码--->与txt文件中账号密码进行验证--->进入下一个界面

# -*- coding: utf-8 -*- from PyQt5 import QtCore, QtGui, QtWidgets class Ui_ok_cancle_Dialog(object):
def setupUi(self, ok_cancle_Dialog):
ok_cancle_Dialog.setObjectName("ok_cancle_Dialog")
ok_cancle_Dialog.resize(411, 305)
ok_cancle_Dialog.setSizeGripEnabled(True)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(ok_cancle_Dialog)
self.horizontalLayout_4.setSizeConstraint(QtWidgets.QLayout.SetMinimumSize)
self.horizontalLayout_4.setSpacing(0)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.frame = QtWidgets.QFrame(ok_cancle_Dialog)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.verticalLayout = QtWidgets.QVBoxLayout(self.frame)
self.verticalLayout.setObjectName("verticalLayout")
self.frame_2 = QtWidgets.QFrame(self.frame)
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.frame_2)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.frame_2)
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.frame_2)
self.lineEdit.setMinimumSize(QtCore.QSize(0, 25))
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.verticalLayout.addWidget(self.frame_2)
self.frame_3 = QtWidgets.QFrame(self.frame)
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.frame_3)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.frame_3)
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame_3)
self.lineEdit_2.setMinimumSize(QtCore.QSize(0, 25))
self.lineEdit_2.setText("")
self.lineEdit_2.setFrame(True)
self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
self.lineEdit_2.setReadOnly(False)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.verticalLayout.addWidget(self.frame_3)
self.label_3 = QtWidgets.QLabel(self.frame)
self.label_3.setMaximumSize(QtCore.QSize(16777215, 20))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.label_3.setFont(font)
self.label_3.setStyleSheet("color: rgb(255, 0, 0);")
self.label_3.setText("")
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.verticalLayout.addWidget(self.label_3)
self.frame_4 = QtWidgets.QFrame(self.frame)
self.frame_4.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_4.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_4.setObjectName("frame_4")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.frame_4)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.pushButton = QtWidgets.QPushButton(self.frame_4)
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("background-color: rgb(116, 255, 155);")
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_3.addWidget(self.pushButton)
spacerItem = QtWidgets.QSpacerItem(30, 20, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_3.addItem(spacerItem)
self.pushButton_2 = QtWidgets.QPushButton(self.frame_4)
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setStyleSheet("background-color: rgb(62, 108, 73);")
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_3.addWidget(self.pushButton_2)
self.verticalLayout.addWidget(self.frame_4)
self.horizontalLayout_4.addWidget(self.frame) self.retranslateUi(ok_cancle_Dialog)
QtCore.QMetaObject.connectSlotsByName(ok_cancle_Dialog) def retranslateUi(self, ok_cancle_Dialog):
_translate = QtCore.QCoreApplication.translate
ok_cancle_Dialog.setWindowTitle(_translate("ok_cancle_Dialog", "Dialog"))
self.label.setText(_translate("ok_cancle_Dialog", "账号:"))
self.label_2.setText(_translate("ok_cancle_Dialog", "密码:"))
self.pushButton.setText(_translate("ok_cancle_Dialog", "确认"))
self.pushButton_2.setText(_translate("ok_cancle_Dialog", "取消")) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ok_cancle_Dialog = QtWidgets.QDialog()
ui = Ui_ok_cancle_Dialog()
ui.setupUi(ok_cancle_Dialog)
ok_cancle_Dialog.show()
sys.exit(app.exec_())
UI文件
# -*- coding: utf-8 -*-
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QDialog
from PyQt5 import QtWidgets
from Ui_ok_cancel import Ui_ok_cancle_Dialog class ok_cancle_Dialog(QDialog, Ui_ok_cancle_Dialog): def __init__(self, parent=None):
super(ok_cancle_Dialog, self).__init__(parent)
self.setupUi(self) @pyqtSlot()
def on_pushButton_clicked(self):
f = open(r'logs\account.txt', 'r+',encoding='utf8') #从logs文件夹下读取account.txt文件 中的账号 密码
data = f.readlines()
confirm = data[0].rstrip('\n') == self.lineEdit.text() and data[1] == self.lineEdit_2.text()
if confirm:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.taobao.com")
browser.maximize_window() else:
#print('=======================')
self.label_3.setText('账号或密码错误请重新输入') f.close() @pyqtSlot()
def on_pushButton_2_clicked(self):
self.lineEdit.setText('')
self.lineEdit_2.setText('')
self.label_3.setText('') if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = ok_cancle_Dialog()
ui.show()
sys.exit(app.exec_())
main文件
【微语】Beauty is found within ---美丽源于内心
【PyQt5-Qt Designer】文本框读写操作的更多相关文章
- Java + selenium 元素定位(6)之iframe切换(即对富文本框的操作)
在元素定位中,对富文本框的元素定位是特别的,当我们使用普通的元素定位方法对富文本框进行操作时,我们会发现不管我们之前介绍的八种方法中的任何方法,我们都不能成功定位到富文本框,并对其进行操作.那是因为富 ...
- python中文本的读写操作
文本的操作 函数的排序操作: def func(i): return i[2] list=[('曹操',101,'c'),('吕布',100,'d'),('刘备',200,'l'),('大乔',50, ...
- PyCharm+PyQt5+Qt Designer配置
配置前提 因为本机已经配置完毕了,本次使用的是虚拟机中的Win7系统,Win10系统操作步骤完全一样,无任何区别 PyCharm (这个不多说,官网下载安装,我是用的是2019.3版本) Python ...
- pyqt5:标签显示文本框内容
文本框(lineEdit)输入文本,标签(label)就会显示文本框的内容. 原理如下: 输入文本时,lineEdit控件发射信号textChanged(),label收到后触发setText()槽. ...
- PyQt5 & Qt Designer使用小结
开始在知乎写文章的原因,主要还是想整理平时的经验,方便自己以后查看,有机会的话大家也可以交流吧. 11月中旬由于项目需要,和另一名实习生负责使用Python开发一个数据分析的小软件. 虽然才开始接触Q ...
- Qt限制文本框输入的方法(使用QRegExpValidator,为QLineEdit所独有)
在做界面编程的时候,对文本框的处理往往是个很头疼的事情,一是焦点进入文本框时,从人性化考虑,应选择文本框中文本,方便输入:二是,限制文本框的输入,只允许输入有效的文本,如果不这样做的话,那么就需要在程 ...
- 对pdf 表单域 或文本框的操作---动态填充PDF 文件内容
前提:需要pdf模板:并且模板内容以pdf 文本框的形式填写 package com.test;import java.io.File;import java.io.FileOutputStream; ...
- C++对txt文本进行读写操作
输入输出,是每个程序员的基本功,尤其是对文本的输入和输出.最近,自己在这方面做了一些总结,不是很全面,希望在以后学习和工作的过程中慢慢补充,积累点点滴滴.P.S. 今天天气不错,雾霾散了,天空晴朗,惠 ...
- FileStream对文本进行读写操作
class FileHelper { /// <summary> /// 检验文件路径是否合法 /// </summary> /// <param name=" ...
随机推荐
- Spring Security 匿名认证
1.项目截图: 2.匿名认证配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- 解决Android8.0之后开启service时报错IllegalStateException: Not allowed to start service Intent ...
项目测试时发现的,在双击返回键关闭应用后(并未杀死后台)重新打开APP,其他手机都OK,但是8.0的手机会出现较频繁的crash.检查代码,问题锁定在重新开启应用时的startService()上. ...
- 在VSCode中成功安装Go相关插件问题:tools failed to install.
一.介绍 目的:本文将主要介绍在windows使用VSCode配置Go语言环境 软件:VSCode 二.安装出现的问题 完整信息如下 Installing tools at D:\GoPath\bin ...
- centos6.5安装mongodb2.6
下载地址:http://www.mongodb.org/downloads 解压命令:tar zxf mongodb-linux-i686-2.6.0.tgz 存放目录:/usr/local/mong ...
- 设置Linux打开文件句柄/proc/sys/fs/file-max和ulimit -n的区别
max-file 表示系统级别的能够打开的文件句柄的数量.是对整个系统的限制,并不是针对用户的. ulimit -n 控制进程级别能够打开的文件句柄的数量.提供对shell及其启动的进程的可用文件句柄 ...
- HashMap和Hashtable的区别 2
导读: 1 HashMap不是线程安全的 hastmap是一个接口 是map接口的子接口,是将键映射到值的对象,其中键和值都是对象,并且不能包含重复键,但可以包含重复值.HashMap允许null k ...
- F - Unix ls
The computer company you work for is introducing a brand new computer line and is developing a new U ...
- PCL Save VTK File With Texture Coordinates 使用PCL库来保存带纹理坐标的VTK文件
我之前有一篇博客Convert PLY to VTK Using PCL 1.6.0 or PCL 1.8.0 使用PCL库将PLY格式转为VTK格式展示了如何将PLY格式文件转化为VTK格式的文件, ...
- win怎么设置最快捷的下滑关机
win怎么设置最快捷的下滑关机 1.在C:\Windows\System32下找到SlideToShutDown.exe文件发送一份到桌面快捷方式 2.右键此快捷方式--属性--更换图表--更换一个自 ...
- atof()函数详解
atof()函数 atof():double atof(const char *str ); 功 能: 把字符串转换成浮点数 str:要转换的字符串. 返回值:每个函数返回 double 值,此值由将 ...