Quantcast
Channel: Jobs
Viewing all articles
Browse latest Browse all 18427

Cut, Copy, and Paste buttons in PyQt4

$
0
0
Hi, I am working on a text editor to release with Cut, Paste, Copy, Exit buttons ‘n’ all. I have made the buttons in my toolbar and Edit menu, with this code for the them:      exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)                 exitAction.setShortcut('Ctrl+Q')         exitAction.setStatusTip('Exit Application')         exitAction.triggered.connect(QtGui.qApp.quit)           copyAction = QtGui.QAction(QtGui.QIcon('copy.png'), '&Copy', self)         copyAction.setShortcut('Ctrl+C')         copyAction.setStatusTip('Copy to the Clipboard')         copyAction.triggered.connect(QtGui.QTextEdit.copy(self))           pasteAction = QtGui.QAction(QtGui.QIcon('paste.png'), '&Paste', self)         pasteAction.setShortcut('Ctrl+V')         pasteAction.setStatusTip('Paste from the Clipboard')         pasteAction.triggered.connect(QtGui.QTextEdit.paste(self)) Unfortunately This does not work, when I have searched the internet for hours and had hours of trial and error, still nothing. My code is as following: import sys import os from PyQt4 import QtGui, QtCore     class Window(QtGui.QMainWindow):         def __init__(self):         super(Window, self).__init__()                 self.initUI()             def center(self):           qr = self.frameGeometry()         cp = QtGui.QDesktopWidget().availableGeometry().center()         qr.moveCenter(cp)         self.move(qr.topLeft())             def initUI(self):           exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)                 exitAction.setShortcut('Ctrl+Q')         exitAction.setStatusTip('Exit Application')         exitAction.triggered.connect(QtGui.qApp.quit)           copyAction = QtGui.QAction(QtGui.QIcon('copy.png'), '&Copy', self)         copyAction.setShortcut('Ctrl+C')         copyAction.setStatusTip('Copy to the Clipboard')         copyAction.triggered.connect(QtGui.QTextEdit.copy(self))           pasteAction = QtGui.QAction(QtGui.QIcon('paste.png'), '&Paste', self)         pasteAction.setShortcut('Ctrl+V')         pasteAction.setStatusTip('Paste from the Clipboard')         pasteAction.triggered.connect(QtGui.QTextEdit.paste(self))           cutAction = QtGui.QAction(QtGui.QIcon('cut.png'), '&Cut', self)         cutAction.setShortcut('Ctrl+X')         cutAction.setStatusTip('Copy text to the clipboard and delet from editor')         cutAction.triggered.connect(QtGui.QTextEdit.cut(self))           self.statusBar()           menubar = self.menuBar()         fileMenu = menubar.addMenu('&File')         fileMenu.addAction(exitAction)         editMenu = menubar.addMenu('&Edit')         editMenu.addAction(copyAction)         editMenu.addAction(pasteAction)         editMenu.addAction(cutAction)           toolbar = self.addToolBar('Exit')         toolbar.addAction(exitAction)         toolbar.addAction(copyAction)         toolbar.addAction(pasteAction)         toolbar.addAction(cutAction)                 self.setGeometry(10, 10, 500, 500)         self.setWindowTitle('Text Editor')         self.setWindowIcon(QtGui.QIcon('favicon.png'))         self.show()         self.center()           edit = QtGui.QTextEdit('', self)         edit.setStatusTip('Input Text Here')         edit.resize(480, 400)         edit.move(10, 60)         edit.setToolTip('Input Text Here')         edit.show()         def closeEvent(self, event):           reply = QtGui.QMessageBox.question(self, 'Message', "Are you sure you would like to quit?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)           if reply == QtGui.QMessageBox.Yes:             event.accept()         else:             event.ignore()         def main():         app = QtGui.QApplication(sys.argv)     ex = Window()     sys.exit(app.exec_())     if __name__ == '__main__':     main() If you could get back to me as soon as possible it would be great! I am really looking forwards to seeing the finished product!

Viewing all articles
Browse latest Browse all 18427

Trending Articles