有没有办法将标记文本添加到 pyqt 中的变量中?

发布于 2024-09-30 12:32:53 字数 801 浏览 3 评论 0原文

我刚刚在大学上完第一门编程课程,在接下来的三个月里我没有额外的编程课程,所以我决定在这个“休息”期间做一个小项目。

我想做的是为我曾经工作过的一个较小的维基百科编写一个编辑程序。它应该让用户更容易地使用模板之类的东西,并且还有一个向导来帮助用户制作基本页面。我和一些年长的学生交谈过,他们推荐使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩心态 2024-10-07 12:32:53
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_()

遗憾的是,我无法找到不操作剪贴板的方法。希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文