想试着写一个类似idle的程序:
源码如下:
import sys
import mainui
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog,QTextBrowser
import subprocess
import win32api
# reply=subprocess.getstatusoutput(cmd="cmd")
# print(reply)
class MainCode(QMainWindow, mainui.Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
mainui.Ui_MainWindow.__init__(self)
self.setupUi(self)
self.pushButton_2.clicked.connect(self.on_save)
self.pushButton.clicked.connect(self.on_open)
self.pushButton_3.clicked.connect(self.open_py)
def on_save(self):
FullFileName, _ = QFileDialog.getSaveFileName(self, '文件另存为', r'./', 'TXT (*.txt)')
if not FullFileName:
return
set_text = self.textEdit.toPlainText()
with open(FullFileName, 'wt') as fp:
print(set_text, file=fp)
def open_py(self):
win32api.ShellExecute(0, 'open', 'python.exe', FullFileName, '', 1)
def on_open(self):
txtstr = ""
FullFileName, _ = QFileDialog.getOpenFileName(self, '打开', r'./', 'TXT (*.txt)')
if not FullFileName:
return
with open(FullFileName, 'rt') as fp:
lines = fp.readlines()
for line in lines:
txtstr = txtstr + line
self.textEdit.setText(txtstr)
# def connect_cmd(self):
# reply = subprocess.getstatusoutput("cmd")
# print(reply)
# def show_cmd(self):
# QTextBrowser.setText()
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = MainCode()
ui.show()
sys.exit(app.exec())
mainui.py:
# -*- 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(1232, 594)
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.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(10, 40, 591, 501))
self.layoutWidget.setObjectName("layoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.textEdit = QtWidgets.QTextEdit(self.layoutWidget)
self.textEdit.setObjectName("textEdit")
self.verticalLayout.addWidget(self.textEdit)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout)
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(630, 40, 131, 41))
self.pushButton_3.setObjectName("pushButton_3")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(630, 120, 461, 391))
self.textBrowser.setObjectName("textBrowser")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(630, 100, 54, 12))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1232, 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", "保存文件"))
self.pushButton_3.setText(_translate("MainWindow", "运行文件"))
self.label_2.setText(_translate("MainWindow", "程序日志"))
想达到的效果:
检测到刚保存的文件,并用根目录下的python,exe来运行此程序,然后在右边的TextBrowser组件中显示事实日志
显示实时日志我可以自己百度,但是“检测到刚保存的文件,并用根目录下的python,exe来运行此程序”这里不是很会写