Is there a way to add marked text into a variable in pyqt?
I've just had my first course in programming at the university and for the following three months I have no additional programming classes so I've decided to do a small project during this "break".
What I'm trying to do is a edit-program for a smaller Wiki I used to work on. It's suppose to make it easier for the users to use things like templates, and also have a wizard to help the user make basic pages. I talked to some older students and they recommended pyqt for the GUI of the software.
Now to the problem, and I feel like this is a really dirty hack: My solution right now is to use the built in copy and paste commands, the problem is that right now if I just click the button开发者_如何转开发 for bold, without marking text, I get: '''text currently in clipboard''' and I just want it to add ''' '''.
Here's the (important) code in question, I obviously call addBold when the button/hotkey is pushed.
self.textEdit = QtGui.QTextEdit()
def.addBold(self):
self.textEdit.copy()
self.textEdit.insertPlainText("\'\'\'")
self.textEdit.paste()
self.textEdit.insertPlainText("\'\'\'")
What I'd rather have is code that looks something like:
x=markedText
if not x:
self.textEdit.insertPlainText("\'\'\' \'\'\'")
else:
self.textEdit.insertPlainText("\'\'\'"+x+"\'\'\'")
x = None
So does anyone know how I can assign the marked text to x? Or is there yet another solution that is better?
from PyQt4.QtGui import *
from PyQt4.QtCore import SIGNAL
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self)
self.textedit = QTextEdit()
self.clip = QApplication.clipboard()
self.button = QPushButton("Bold")
self.connect(self.button, SIGNAL("clicked()"), self.addBold)
layout = QVBoxLayout()
layout.addWidget(self.textedit)
layout.addWidget(self.button)
self.setLayout(layout)
def addBold(self):
self.clip.clear()
self.textedit.copy()
currentText = self.clip.text()
self.textedit.insertPlainText("'''%s'''" % currentText)
app = QApplication([])
widget = Widget()
widget.show()
app.exec_()
Sadly I could not find a way without manipulating the clipboard. Hope this helps.
精彩评论