to install (via wget) and use:
http://www.riverbankcomputing.co.uk/software/pyqt/download
$ python p-edit.py
for more:
e-mail patx: patx44 at gmail dot com
website: http://bitbucket.org/patx/p-edit
# p-Edit by PATX.
# With help from:
# Carson J. Q. Farmer
# & rowinggolfer
"""
Copyright (c) 2009 Harrison Erd
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
"""
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtCore, QtGui
class MyHighlighter( QSyntaxHighlighter ):
def __init__( self, parent, theme ):
QSyntaxHighlighter.__init__( self, parent )
self.parent = parent
keyword = QTextCharFormat()
reservedClasses = QTextCharFormat()
assignmentOperator = QTextCharFormat()
delimiter = QTextCharFormat()
specialConstant = QTextCharFormat()
boolean = QTextCharFormat()
number = QTextCharFormat()
comment = QTextCharFormat()
string = QTextCharFormat()
singleQuotedString = QTextCharFormat()
self.highlightingRules = []
# keyword
brush = QBrush( Qt.darkBlue, Qt.SolidPattern )
keyword.setForeground( brush )
keyword.setFontWeight( QFont.Bold )
keywords = QStringList( [ "break", "else", "for", "if", "in",
"next", "repeat", "return", "switch",
"try", "while", "self" ] )
for word in keywords:
pattern = QRegExp("\\b" + word + "\\b")
rule = HighlightingRule( pattern, keyword )
self.highlightingRules.append( rule )
# reservedClasses
reservedClasses.setForeground( brush )
reservedClasses.setFontWeight( QFont.Bold )
keywords = QStringList( [ "array", "character", "complex",
"data.frame", "double", "factor",
"function", "integer", "list",
"logical", "matrix", "numeric",
"vector" ] )
for word in keywords:
pattern = QRegExp("\\b" + word + "\\b")
rule = HighlightingRule( pattern, reservedClasses )
self.highlightingRules.append( rule )
# assignmentOperator
brush = QBrush( Qt.yellow, Qt.SolidPattern )
pattern = QRegExp( "(<){1,2}-" )
assignmentOperator.setForeground( brush )
assignmentOperator.setFontWeight( QFont.Bold )
rule = HighlightingRule( pattern, assignmentOperator )
self.highlightingRules.append( rule )
# delimiter
pattern = QRegExp( "[\)\(]+|[\{\}]+|[][]+" )
delimiter.setForeground( brush )
delimiter.setFontWeight( QFont.Bold )
rule = HighlightingRule( pattern, delimiter )
self.highlightingRules.append( rule )
# specialConstant
brush = QBrush( Qt.green, Qt.SolidPattern )
specialConstant.setForeground( brush )
keywords = QStringList( [ "Inf", "NA", "NaN", "NULL" ] )
for word in keywords:
pattern = QRegExp("\\b" + word + "\\b")
rule = HighlightingRule( pattern, specialConstant )
self.highlightingRules.append( rule )
# boolean
boolean.setForeground( brush )
keywords = QStringList( [ "TRUE", "FALSE" ] )
for word in keywords:
pattern = QRegExp("\\b" + word + "\\b")
rule = HighlightingRule( pattern, boolean )
self.highlightingRules.append( rule )
# number
pattern = QRegExp( "[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?" )
pattern.setMinimal( True )
number.setForeground( brush )
rule = HighlightingRule( pattern, number )
self.highlightingRules.append( rule )
# comment
brush = QBrush( Qt.blue, Qt.SolidPattern )
pattern = QRegExp( "#[^\n]*" )
comment.setForeground( brush )
rule = HighlightingRule( pattern, comment )
self.highlightingRules.append( rule )
# string
brush = QBrush( Qt.red, Qt.SolidPattern )
pattern = QRegExp( "\".*\"" )
pattern.setMinimal( True )
string.setForeground( brush )
rule = HighlightingRule( pattern, string )
self.highlightingRules.append( rule )
# singleQuotedString
pattern = QRegExp( "\'.*\'" )
pattern.setMinimal( True )
singleQuotedString.setForeground( brush )
rule = HighlightingRule( pattern, singleQuotedString )
self.highlightingRules.append( rule )
def highlightBlock( self, text ):
for rule in self.highlightingRules:
expression = QRegExp( rule.pattern )
index = expression.indexIn( text )
while index >= 0:
length = expression.matchedLength()
self.setFormat( index, length, rule.format )
index = text.indexOf( expression, index + length )
self.setCurrentBlockState( 0 )
class HighlightingRule():
def __init__( self, pattern, format ):
self.pattern = pattern
self.format = format
class pedit(QtGui.QWidget):
def Save(self):
save_file = QtGui.QFileDialog.getSaveFileName(self,'Save File','')
myfile=open(save_file,'w')
data=self.txt.toPlainText()
myfile.write(data)
myfile.close()
def Open(self):
open_file = QtGui.QFileDialog.getOpenFileName(self,'Open File','')
open_the_file = open(open_file, 'r')
self.txt.setPlainText(open_the_file.read())
open_the_file.close()
def __init__(self):
QtGui.QWidget.__init__(self)
self.pb1 = QtGui.QPushButton('Save')
self.pb2 = QtGui.QPushButton('Open')
self.txt = QtGui.QTextEdit()
highlighter = MyHighlighter( self.txt, "Classic" )
tab = QtGui.QGridLayout()
tab.setSpacing(5)
tab.addWidget(self.txt, 2, 0, 1, 2)
tab.addWidget(self.pb1, 0, 0)
tab.addWidget(self.pb2, 0, 1)
self.setLayout(tab)
self.setWindowTitle('p-Edit')
self.txt.setStyleSheet('font-family: monospace')
self.connect(self.pb1, QtCore.SIGNAL('clicked()'),
self.Save)
self.connect(self.pb2, QtCore.SIGNAL('clicked()'),
self.Open)
if __name__ == "__main__":
app = QApplication( sys.argv )
window = pedit()
window.show()
sys.exit( app.exec_() )
No problem, I appreciate the Python snippets. It's great to see people posting in other languages on the site, they inspire me to learn new languages and platforms. The new button location is an improvement, but I think what would be ideal are application menus like File, Edit, etc. I don't know if you can do this with PyQt, but it would be nice.
@Hawkee I have changed it so there Open and Save buttons (the only buttons) are on the top. Once again thanks for your feedback on these GUIs I have made most of them (this and the twitter client) when I was bored... It really helps having a person comment on these that way they can improve and more importantly i can improve. Thanks again -- PATX