PyQt5結(jié)合QtDesigner實(shí)現(xiàn)文本框讀寫(xiě)操作
本文主要介紹了PyQt5結(jié)合QtDesigner實(shí)現(xiàn)文本框讀寫(xiě)操作,分享給大家,具體如下:
主要內(nèi)容:
1、讀、寫(xiě) 輸入控件(Input Widgets)中的內(nèi)容(str)
2、保存數(shù)據(jù)到txt文件
3、從txt文件中讀內(nèi)容,與輸入控件中內(nèi)容比較
將上述各種輸入控件(Input Widgets)中的內(nèi)容保存到txt文件中:
Ui文件
# -*- 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", "123")) 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_())
Main文件
# -*- 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文件
實(shí)戰(zhàn)案例:
登錄框--->輸入賬號(hào)密碼--->與txt文件中賬號(hào)密碼進(jìn)行驗(yàn)證--->進(jìn)入下一個(gè)界面
UI文件
# -*- 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", "賬號(hào):")) self.label_2.setText(_translate("ok_cancle_Dialog", "密碼:")) self.pushButton.setText(_translate("ok_cancle_Dialog", "確認(rèn)")) 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_())
main文件
# -*- 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文件 中的賬號(hào) 密碼 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('賬號(hào)或密碼錯(cuò)誤請(qǐng)重新輸入') 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_())
到此這篇關(guān)于PyQt5結(jié)合QtDesigner實(shí)現(xiàn)文本框讀寫(xiě)操作的文章就介紹到這了,更多相關(guān)PyQt5 文本框讀寫(xiě)操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡(jiǎn)單了解Python讀取大文件代碼實(shí)例
這篇文章主要介紹了簡(jiǎn)單了解Python讀取大文件代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Python Pandas中根據(jù)列的值選取多行數(shù)據(jù)
這篇文章主要介紹了Python Pandas中根據(jù)列的值選取多行數(shù)據(jù)的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì) ,需要的朋友可以參考下2019-07-07使用 Visual Studio Code(VSCode)搭建簡(jiǎn)單的Python+Djan
這篇文章主要介紹了使用 Visual Studio Code(VSCode)搭建簡(jiǎn)單的Python+Django開(kāi)發(fā)環(huán)境的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12如何利用Python實(shí)現(xiàn)自動(dòng)打卡簽到的實(shí)踐
簽到,都是規(guī)律性的操作,何嘗不寫(xiě)一個(gè)程序加到Windows實(shí)現(xiàn)自動(dòng)簽到呢,本文就主要介紹了如何利用Python實(shí)現(xiàn)自動(dòng)打卡簽到的實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2021-12-12Python的Flask框架應(yīng)用調(diào)用Redis隊(duì)列數(shù)據(jù)的方法
這里為大家?guī)?lái)Python的Flask框架應(yīng)用調(diào)用Redis隊(duì)列數(shù)據(jù)的方法,從而能夠?qū)崿F(xiàn)異步無(wú)阻塞從而提高某些實(shí)時(shí)處理情況下程序的性能,需要的朋友可以參考下2016-06-06pandas中read_sql使用參數(shù)進(jìn)行數(shù)據(jù)查詢的實(shí)現(xiàn)
本文主要介紹了pandas中read_sql使用參數(shù)進(jìn)行數(shù)據(jù)查詢的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Python Scrapy實(shí)戰(zhàn)之古詩(shī)文網(wǎng)的爬取
本文將利用Python中Scrapy框架,實(shí)現(xiàn)爬取古詩(shī)文網(wǎng)上的詩(shī)詞數(shù)據(jù),具體包括詩(shī)詞的標(biāo)題信息。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-05-05