PyQt 5 는 QtDesigner 와 결합 하여 텍스트 상자 읽 기와 쓰기 동작 을 실현 합 니 다.

본 고 는 주로 PyQt 5 가 QtDesigner 와 결합 하여 텍스트 상자 의 읽 기와 쓰기 동작 을 실현 하고 여러분 에 게 공유 하 는 것 을 소개 합 니 다.구체 적 으로 다음 과 같 습 니 다.
주요 내용:
1.입력 컨트롤(Input Widgets)의 내용 읽 기,쓰기(str)
2.txt 파일 에 데 이 터 를 저장 합 니 다.
3.txt 파일 에서 내용 을 읽 고 입력 컨트롤 의 내용 과 비교 합 니 다.

위 와 같은 각종 입력 컨트롤(Input Widgets)의 내용 을 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\">
" "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">
" "p, li { white-space: pre-wrap; }
" "</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">
" "<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_())
주 파일

# -*- 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()+'
') f.write(self.lineEdit.text()+'
') f.write(self.plainTextEdit.toPlainText()+'
') f.write(self.comboBox.currentText()+'
') f.write(self.fontComboBox.currentText()+'
') f.write(self.fontComboBox.currentText()+'
') f.write(str(self.spinBox.value())+'
') f.write(str(self.doubleSpinBox.value())+'
') if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) ui = file_dailog() ui.show() sys.exit(app.exec_()) Main
실전 사례:
로그 인 상자->계 정 비밀번호 입력->txt 파일 의 계 정 비밀번호 와 검증->다음 인터페이스 로 들 어가 기

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", "  :"))
        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_())
주 파일

# -*- 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('
') == 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_())
PyQt 5 가 QtDesigner 와 결합 하여 텍스트 상자 읽 기와 쓰기 동작 을 수행 하 는 것 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 관련 PyQt 5 텍스트 상자 읽 기와 쓰기 동작 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 지원 바 랍 니 다!

좋은 웹페이지 즐겨찾기