写了一个pyqt5的界面
代码如下:
import sys
import mainui
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
# import os
# os.environ["CUDA_VISIBLE_DEVICES"]="-1"
class MainCode(QMainWindow, mainui.Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
mainui.Ui_MainWindow.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.on_save)
self.pushButton_2.clicked.connect(self.on_open)
def on_save(self):
FullFileName, _ = QFileDialog.getSaveFileName(self, '文件另存为', r'./', 'TXT (*.txt)')
set_text = self.txt_view.toPlainText()
with open(FullFileName, 'wt') as fp:
print(set_text, file=fp)
def on_open(self):
txtstr = ""
FullFileName, _ = QFileDialog.getOpenFileName(self, '打开', r'./', 'TXT (*.txt)')
with open(FullFileName, 'rt') as fp:
lines = fp.readlines()
for line in lines:
txtstr = txtstr + line
self.txt_view.setText(txtstr)
if __name__ == '__main__':
app = QApplication(sys.argv)
ui=MainCode()
# ui.setupUi(mainwindows)
# ui.setupUi(ui)
ui.show()
sys.exit(app.exec())
ui文件代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainui.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 121, 31))
self.label.setObjectName("label")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(10, 50, 771, 501))
self.widget.setObjectName("widget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.textBrowser = QtWidgets.QTextBrowser(self.widget)
self.textBrowser.setObjectName("textBrowser")
self.verticalLayout.addWidget(self.textBrowser)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.widget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "请在此输入:"))
self.pushButton.setText(_translate("MainWindow", "保存文件"))
self.pushButton_2.setText(_translate("MainWindow", "选择文件"))
程序运行后,点击任意一个按钮后直接卡死
且文本框无法输入文本,这是为什么呢?
响应结果如下:
进程已结束,退出代码-1073740791 (0xC0000409)