Skip to content

code_block

_code_block

Code block widget with syntax highlighting.

Uses Pygments for syntax highlighting, supporting 500+ programming languages.

Classes:

Name Description
FXCodeBlock

A code block widget with syntax highlighting and theme-aware styling.

FXPygmentsHighlighter

Syntax highlighter using Pygments for multi-language support.

Functions:

Name Description
example

Example demonstrating the FXCodeBlock widget with multiple languages.

get_supported_languages

Get a list of all supported language names.

Classes

FXCodeBlock

FXCodeBlock(
    code: str = "", language: str = "python", parent: Optional[QWidget] = None
)

Bases: FXThemeAware, QWidget


              flowchart TD
              fxgui.fxwidgets._code_block.FXCodeBlock[FXCodeBlock]
              fxgui.fxstyle.FXThemeAware[FXThemeAware]

                              fxgui.fxstyle.FXThemeAware --> fxgui.fxwidgets._code_block.FXCodeBlock
                


              click fxgui.fxwidgets._code_block.FXCodeBlock href "" "fxgui.fxwidgets._code_block.FXCodeBlock"
              click fxgui.fxstyle.FXThemeAware href "" "fxgui.fxstyle.FXThemeAware"
            

A code block widget with syntax highlighting and theme-aware styling.

This widget displays code with: - Syntax highlighting for 500+ languages via Pygments - Theme-aware background and text colors - Monospace font - Read-only, selectable text

Parameters:

Name Type Description Default
code
str

The code string to display.

''
language
str

The programming language (e.g., "python", "javascript").

'python'
parent
Optional[QWidget]

The parent widget.

None
Example

code = ''' ... def hello(): ... print("Hello, World!") ... ''' code_block = FXCodeBlock(code)

Methods:

Name Description
code

Get the current code.

set_code

Set the code to display.

set_language

Set the programming language for syntax highlighting.

Functions
code
code() -> str

Get the current code.

Returns:

Type Description
str

The code string.

set_code
set_code(code: str) -> None

Set the code to display.

Parameters:

Name Type Description Default
code str

The code string.

required
set_language
set_language(language: str) -> None

Set the programming language for syntax highlighting.

Parameters:

Name Type Description Default
language str

The language name. Supports 500+ languages via Pygments (e.g., "python", "javascript", "cpp", "rust", "go", "java"). Use get_supported_languages() to see all available options.

required

FXPygmentsHighlighter

FXPygmentsHighlighter(document, language: str = 'python')

Bases: FXThemeAware, QSyntaxHighlighter


              flowchart TD
              fxgui.fxwidgets._code_block.FXPygmentsHighlighter[FXPygmentsHighlighter]
              fxgui.fxstyle.FXThemeAware[FXThemeAware]

                              fxgui.fxstyle.FXThemeAware --> fxgui.fxwidgets._code_block.FXPygmentsHighlighter
                


              click fxgui.fxwidgets._code_block.FXPygmentsHighlighter href "" "fxgui.fxwidgets._code_block.FXPygmentsHighlighter"
              click fxgui.fxstyle.FXThemeAware href "" "fxgui.fxstyle.FXThemeAware"
            

Syntax highlighter using Pygments for multi-language support.

This highlighter uses Pygments lexers and styles to tokenize and format code. It leverages Pygments' built-in style system for consistent token coloring.

Parameters:

Name Type Description Default
document

The QTextDocument to highlight.

required
language
str

The programming language name (e.g., "python", "javascript").

'python'
Example

highlighter = FXPygmentsHighlighter(text_edit.document(), "python")

Methods:

Name Description
highlightBlock

Apply syntax highlighting to a block of text.

language

Get the current language.

refresh_formats

Refresh formats when theme changes.

set_language

Change the syntax highlighting language.

Functions
highlightBlock
highlightBlock(text: str) -> None

Apply syntax highlighting to a block of text.

Parameters:

Name Type Description Default
text str

The text to highlight.

required
language
language() -> str

Get the current language.

Returns:

Type Description
str

The current language name.

refresh_formats
refresh_formats() -> None

Refresh formats when theme changes.

set_language
set_language(language: str) -> None

Change the syntax highlighting language.

Parameters:

Name Type Description Default
language str

The programming language name.

required

Functions

example

example() -> None

Example demonstrating the FXCodeBlock widget with multiple languages.

get_supported_languages

get_supported_languages() -> list[str]

Get a list of all supported language names.

Returns:

Type Description
list[str]

A sorted list of language names that can be used with FXCodeBlock.

Example

languages = get_supported_languages() "python" in languages True "javascript" in languages True