【问题标题】:PyQt5 Equation Text Box (Subscript/Superscript)PyQt5 方程文本框(下标/上标)
【发布时间】:2020-11-11 00:25:31
【问题描述】:

我正在使用 PyQt5 制作一个化学应用程序,我想实现一个方程式文本框,如果用户按下 Shift+6,它将转到下标等。我研究了很多,但找不到很好的解释如何做到这一点。我真的很想要这个功能,任何帮助都将不胜感激。

【问题讨论】:

    标签: python-3.x user-interface pyqt5


    【解决方案1】:

    你可以做这样的事情。在本例中,当按下 Shift-F6 时,文本框中的任何选定文本都将变为下标(如果选择已经是下标,则返回正常)。

    from PyQt5 import QtWidgets
    from PyQt5.QtCore import Qt
    from PyQt5.QtGui import QTextCharFormat
    
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, parent = None):
            super().__init__(parent)
            self.text_box = QtWidgets.QTextEdit(self)
            layout = QtWidgets.QVBoxLayout(self)
            layout.addWidget(self.text_box)
    
        def keyPressEvent(self, event):
            if event.modifiers() == Qt.ShiftModifier and event.key() == Qt.Key_F6:
                cursor = self.text_box.textCursor()
                format = cursor.charFormat()
                if format.verticalAlignment() == QTextCharFormat.AlignSubScript:
                    vert = QTextCharFormat.AlignNormal
                else:
                    vert = QTextCharFormat.AlignSubScript
                format.setVerticalAlignment(vert)
                cursor.setCharFormat(format)
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication([])
        w = Widget()
        w.show()
        app.exec()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 2014-07-07
      • 2011-05-28
      • 1970-01-01
      • 2019-05-02
      • 2021-08-11
      • 1970-01-01
      相关资源
      最近更新 更多