from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, \
QLabel, QVBoxLayout, QWidget
from PyQt4 import QtGui
import sys
import subprocess
class MainWindow1(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
button = QPushButton('NotePad')
label = QLabel('MainWindow1')
centralWidget = QWidget()
vbox = QVBoxLayout(centralWidget)
vbox.addWidget(label)
vbox.addWidget(button)
self.setCentralWidget(centralWidget)
button.clicked.connect(self.LaunchNotepad)
# Some code here - including import subprocess
def LaunchNotepad(self):
returncode = subprocess.call(['python', 'notepad.py'])
if __name__ == '__main__':
app = QApplication(sys.argv)
mainwindow1 = MainWindow1()
mainwindow1.show()
sys.exit(app.exec_())
该代码创建一个带有按钮的主窗口,当我按下按钮时,我希望它导入我的名为“记事本”的文件,(我认为它确实如此)但是它会打开它并立即关闭它。我需要能够使用程序记事本,直到我关闭它,在这种情况下它应该恢复到原始窗口。最终我将有 3 或 4 个按钮导入 3 或 4 个不同的程序
我认为记事本没有错误,因为当我只有“导入记事本”语句时,它运行得很好
注意:记事本文件只是一个简单的文本程序(很像 Windows 电脑上的“记事本”程序)
提前致谢
编辑这里是记事本代码:
import sys
import os
import datetime as dt
from PyQt4 import QtGui
from PyQt4 import *
class Notepad(QtGui.QMainWindow):
def __init__(self):
super(Notepad, self).__init__()
self.initUI()
def initUI(self):
newAction = QtGui.QAction('New', self)
newAction.setShortcut('Ctrl+N')
newAction.setStatusTip('Create new file')
newAction.triggered.connect(self.newFile)
saveAction = QtGui.QAction('Save', self)
saveAction.setShortcut('Ctrl+S')
saveAction.setStatusTip('Save current file')
saveAction.triggered.connect(self.saveFile)
openAction = QtGui.QAction('Open', self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Open a file')
openAction.triggered.connect(self.openFile)
closeAction = QtGui.QAction('Close', self)
closeAction.setShortcut('Ctrl+Q')
closeAction.setStatusTip('Close Notepad')
closeAction.triggered.connect(self.close)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(newAction)
fileMenu.addAction(saveAction)
fileMenu.addAction(openAction)
fileMenu.addAction(closeAction)
#help menu
helpMenu = menubar.addMenu('&Help')
aboutAction = QtGui.QAction('About', self)
aboutAction.setShortcut('Ctrl+A')
aboutAction.setStatusTip('About')
helpMenu.addAction(aboutAction)
aboutAction.triggered.connect(self.about)
self.text = QtGui.QTextEdit(self)
self.setCentralWidget(self.text)
self.setGeometry(300,300,300,300)
self.setWindowTitle('Notepad')
self.show()
self.statusBar()
def newFile(self):
self.text.clear()
def saveFile(self):
filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', os.getenv('HOME'))
f = open(filename, 'w')
filedata = self.text.toPlainText()
f.write(filedata)
f.close()
def openFile(self):
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', os.getenv('HOME'))
f = open(filename, 'r')
filedata = f.read()
self.text.setText(filedata)
f.close()
self.setGeometry(300,300,300,300)
self.setWindowTitle('Notepad')
self.show()
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self, 'Message',
"Are you sure to quit?", QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def about(self, event):
reply = QtGui.QMessageBox.question(self, 'About Task Manager',
"This is a notepad todo list program written by craig murch")
return Notepad
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
notepad = Notepad()
sys.exit(app.exec_())
编辑:上面的编辑代码现在确实希望我这样做,但是它也加载了我不想要的 cmd,我如何停止它加载 cmd?