有没有办法将标记文本添加到 pyqt 中的变量中?
我刚刚在大学上完第一门编程课程,在接下来的三个月里我没有额外的编程课程,所以我决定在这个“休息”期间做一个小项目。
我想做的是为我曾经工作过的一个较小的维基百科编写一个编辑程序。它应该让用户更容易地使用模板之类的东西,并且还有一个向导来帮助用户制作基本页面。我和一些年长的学生交谈过,他们推荐使用 pyqt 作为软件的 GUI。
现在问题来了,我觉得这是一个非常肮脏的黑客: 我现在的解决方案是使用内置的复制和粘贴命令,问题是,现在如果我只需单击粗体按钮,而不标记文本,我会得到: '''当前在剪贴板中的文本''',我只想添加''' '''。
这是有问题的(重要)代码,当按下按钮/热键时,我显然会调用 addBold 。
self.textEdit = QtGui.QTextEdit()
def.addBold(self):
self.textEdit.copy()
self.textEdit.insertPlainText("\'\'\'")
self.textEdit.paste()
self.textEdit.insertPlainText("\'\'\'")
我宁愿拥有看起来像这样的代码:
x=markedText
if not x:
self.textEdit.insertPlainText("\'\'\' \'\'\'")
else:
self.textEdit.insertPlainText("\'\'\'"+x+"\'\'\'")
x = None
那么有谁知道如何将标记的文本分配给 x?或者还有其他更好的解决方案吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遗憾的是,我无法找到不操作剪贴板的方法。希望这有帮助。
Sadly I could not find a way without manipulating the clipboard. Hope this helps.