#!/usr/bin/python
#--coding:GBK--
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys,os
class MainWindow(QMainWindow):
def __init__(self,parent=None):
#super(MainWindow,self).__init__(parent)
QMainWindow.__init__(self,parent)
self.setWindowTitle("QMainWindow")
self.setWindowIcon(QIcon(r"C:\shiquan\PyQt\new_test\pic\winlearn.png"))
self.text=QTextEdit()
self.setCentralWidget(self.text)
self.createActions()
self.createMenus()
self.createToolBars()
def createActions(self):
self.fileOpenAction=QAction(QIcon(r"C:\shiquan\PyQt\new_test\pic\beyone.png"),"打开",self)
self.fileOpenAction.setShortcut("Ctrl+O")
self.fileOpenAction.setStatusTip("打开一个文件")
self.connect(self.fileOpenAction,SIGNAL("triggered()"),self.slotOpenFile)
self.fileNewAction=QAction(QIcon(r"C:\shiquan\PyQt\new_test\pic\cc.png"),"新建",self)
self.fileNewAction.setShortcut("Ctrl+N")
self.fileNewAction.setStatusTip("新建一个文件")
self.connect(self.fileNewAction,SIGNAL("triggered()"),self.slotNewFile)
self.fileSaveAction=QAction(QIcon(r"C:\shiquan\PyQt\new_test\pic\ctrix.png"),"保存",self)
self.fileSaveAction.setShortcut("Ctrl+S")
self.fileSaveAction.setStatusTip("保存文件")
self.connect(self.fileSaveAction,SIGNAL("triggered()"),self.slotSaveFile)
self.exitAction=QAction(QIcon(r"C:\shiquan\PyQt\new_test\pic\flash.png"),"退出",self)
self.exitAction.setShortcut("Ctrl+Q")
self.setStatusTip("退出")
self.connect(self.exitAction,SIGNAL("triggered()"),self.close)
self.cutAction=QAction(QIcon(r"C:\shiquan\PyQt\new_test\pic\intel.png"),"剪切",self)
self.cutAction.setShortcut("Ctrl+X")
self.cutAction.setStatusTip("剪切到粘贴板")
self.connect(self.cutAction,SIGNAL("triggered()"),self.text.cut)
self.copyAction=QAction(QIcon(r"C:\shiquan\PyQt\new_test\pic\pdf.png"),"复制",self)
self.copyAction.setShortcut("Ctrl+C")
self.copyAction.setStatusTip("复制到粘贴板")
self.connect(self.copyAction,SIGNAL("triggered()"),self.text.copy)
self.pasteAction=QAction(QIcon(r"C:\shiquan\PyQt\new_test\pic\pad.png"),"粘贴",self)
self.pasteAction.setShortcut("Ctrl+V")
self.pasteAction.setStatusTip("粘贴内容到当前处")
self.connect(self.pasteAction,SIGNAL("triggered()"),self.text.paste)
self.aboutAction=QAction(QIcon(r"C:\shiquan\PyQt\new_test\pic\flash.png"),"关于",self)
self.connect(self.aboutAction,SIGNAL("triggered()"),self.slotAbout)
def createMenus(self):
fileMenu=self.menuBar().addMenu("文件")
newMenu = QMenu(fileMenu)
newMenu.setTitle("New")
newMenu.addAction("New Word")
newMenu.addAction("New Excel")
fileMenu.addMenu(newMenu)
fileMenu.addAction(self.fileNewAction)
fileMenu.addAction(self.fileOpenAction)
fileMenu.addAction(self.fileSaveAction)
fileMenu.addAction(self.exitAction)
editMenu=self.menuBar().addMenu("编辑")
editMenu.addAction(self.copyAction)
editMenu.addAction(self.cutAction)
editMenu.addAction(self.pasteAction)
aboutMenu=self.menuBar().addMenu("帮助")
aboutMenu.addAction(self.aboutAction)
def createToolBars(self):
fileToolBar=self.addToolBar("File")
fileToolBar.addAction(self.fileNewAction)
fileToolBar.addAction(self.fileOpenAction)
fileToolBar.addAction(self.fileSaveAction)
editTool=self.addToolBar("Edit")
editTool.addAction(self.copyAction)
editTool.addAction(self.cutAction)
editTool.addAction(self.pasteAction)
aboutTool=self.addToolBar("About")
aboutTool.addAction(self.aboutAction)
def slotNewFile(self):
#newWin=MainWindow()
#newWin.show()
os.popen("notepad")
def slotOpenFile(self):
fileName=QFileDialog.getOpenFileName(self,"Open file dialog",".","Python file:(*.py)")
if os.path.exists(fileName):
data = open(fileName).read()
print ("You select file:",fileName)
self.text.setText(data)
else:
print("No file sepcified!")
pass
def loadFile(self,fileName):
file=QFile(fileName)
if file.open(QIODevice.ReadOnly|QIODevice.Text):
textStream=QTextStream(file)
while textStream.atEnd()==False:
self.text.append(textStream.readLine())
def slotSaveFile(self):
#pass
save_fileName = QFileDialog.getSaveFileName(self,"Save Image",".","Image Files(*.py)")
self.text.setText(save_fileName)
def slotAbout(self):
QMessageBox.about(self,"about me","这是我们的第一个例子")
def closeEvent(self,event):
reply = QMessageBox.question(self,"Message","Are you sure to quit?",QMessageBox.Yes,QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
app=QApplication(sys.argv)
main=MainWindow()
main.resize(500,600)
main.show()
app.exec_()