File: code_syntax_highlight.md

package info (click to toggle)
superqt 0.7.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,320 kB
  • sloc: python: 9,108; makefile: 16; sh: 12
file content (52 lines) | stat: -rw-r--r-- 1,200 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# CodeSyntaxHighlight

A code highlighter subclass of `QSyntaxHighlighter`
that can be used to highlight code in a QTextEdit.

Code lexer and available styles are from [`pygments`](https://pygments.org/) python library

List of available languages are available [here](https://pygments.org/languages/).

List of available styles are available [here](https://pygments.org/styles/).

## Example

```python
from qtpy.QtGui import QColor, QPalette
from qtpy.QtWidgets import QApplication, QTextEdit

from superqt.utils import CodeSyntaxHighlight

app = QApplication([])

text_area = QTextEdit()

highlight = CodeSyntaxHighlight(text_area.document(), "python", "monokai")

palette = text_area.palette()
palette.setColor(QPalette.Base, QColor(highlight.background_color))
text_area.setPalette(palette)
text_area.setText(
    """from argparse import ArgumentParser

def main():
    parser = ArgumentParser()
    parser.add_argument("name", help="Your name")
    args = parser.parse_args()
    print(f"Hello {args.name}")


if __name__ == "__main__":
    main()
"""
)

text_area.show()
text_area.resize(400, 200)

app.exec_()
```

{{ show_widget() }}

{{ show_members('superqt.utils.CodeSyntaxHighlight') }}