PyQtを利用してシンプルなテキストエディタを作ってみた

PyQtを利用してのシンプルなテキストエディタを作ってみました。

```Python:Simpletextedit.py

# -*- coding:utf-8 -*-
# Very Simple Text Editor

# Initial setting & import library
import sys, codecs
from PyQt4 import QtGui, QtCore, QtWebKit
app = QtGui.QApplication(sys.argv)


# Main Window
class CustomMainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(CustomMainWindow, self).__init__(parent)

	# Set Window Style
        self.resize(800, 500)
        self.setWindowTitle("SimpleTextEditor")
        self.setStyleSheet("background-color: white")
	# Enable file drop
        self.setAcceptDrops(True)
	# Add text editer
        self.addtextedit()


        # Tool Bar
        self.file_tool = self.addToolBar("File")
        self.edit_tool = self.addToolBar("Exit")

        newbutton = self.file_tool.addAction("New...")
	self.connect(newbutton, QtCore.SIGNAL('triggered()'), self.addtextedit)

        openbutton = self.file_tool.addAction("Open...")
	self.connect(openbutton, QtCore.SIGNAL('triggered()'), self.showFileDialog)

        closebutton = self.edit_tool.addAction("Save...")
	self.connect(closebutton, QtCore.SIGNAL('triggered()'), self.saveFile)

        closebutton = self.edit_tool.addAction("Close...")
	self.connect(closebutton, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))

        # Add MenuBar
        filemenu = self.menuBar()
        filemenu = filemenu.addMenu('&File')
	filemenu.addMenu("New...")
        filemenu.addMenu("Open...")
        filemenu.addMenu("Save...")

	# Add Open menu
        a = QtGui.QAction('Open', self)
        a.setShortcut('Ctrl+o')
        self.connect(a, QtCore.SIGNAL('triggered()'),
                     self.showFileDialog)
        filemenu.addAction(a)
        
        # Add Exit menu
        a = QtGui.QAction('Exit', self)
        a.setShortcut('Ctrl+w')
        self.connect(a, QtCore.SIGNAL('triggered()'),
                     QtCore.SLOT('close()'))
        filemenu.addAction(a)

	# Add Save menu
        a = QtGui.QAction('Save', self)
        a.setShortcut('Ctrl+s')
        self.connect(a, QtCore.SIGNAL('triggered()'),
                     self.saveFile)
        filemenu.addAction(a)

    # Setting Dialog
    def showFileDialog(self):
        # Add text Editer
        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)

        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open', '.')
        f = open(unicode(fname))
        self.textEdit.setText(f.read())

    # Accept drag of file
    def dragEnterEvent(self, e):
        print("c")
        if e.mimeData().hasUrls():
            e.accept()
        else:
            e.ignore()

    # Drop processing
    def dropEvent(self, e):

        # Add text Editer
        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)

	# Open drop files
        for u in e.mimeData().urls():
            p = unicode(u.toLocalFile())
	    print ("Open file at " + p)
            f = open(unicode(p))
            self.textEdit.setText(f.read())

    # add save file dialog
    def saveFile(self):
        savename = unicode(QtGui.QFileDialog.getSaveFileName(self, 'Save file', ''))
        fname = codecs.open(savename,'w','utf-8')
	fname.write(self.textEdit.toPlainText())

    # add Central Widget => textedit
    def addtextedit(self):
	# Add text Editer
        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)

# Create instance
def main():
    main = CustomMainWindow()
    main.show()
    app.exec_()

if __name__ == '__main__':
    main()
```