### Basic HTMLLabel Usage in Tkinter Source: https://github.com/bauripalash/tkhtmlview/blob/main/README.md Demonstrates how to create and display an HTMLLabel widget with basic HTML styling. Ensure tkinter and tkhtmlview are installed. ```python import tkinter as tk from tkhtmlview import HTMLLabel root = tk.Tk() html_label = HTMLLabel(root, html='
A lightweight HTML rendering library for tkinter applications.
from tkhtmlview import HTMLLabel
import tkinter as tk
root = tk.Tk()
label = HTMLLabel(root, html='<h1>Hello</h1>')
label.pack()
root.mainloop()
Click the navigation buttons above to explore more features.
""") def show_features(self): self.content.set_html("""| Feature | Status |
|---|---|
| HTML Parsing | Supported |
| CSS Styles | Partial |
| Images | Supported |
This paragraph demonstrates multiple CSS properties applied together.
""") if __name__ == "__main__": root = tk.Tk() app = HTMLViewer(root) root.mainloop() ``` -------------------------------- ### Rendering HTML from a File with HTMLText Source: https://github.com/bauripalash/tkhtmlview/blob/main/README.md Shows how to load and render HTML content from a local file into an HTMLText widget. This is useful for managing larger HTML structures separately. The RenderHTML utility is used for this purpose. ```html
The orange is the fruit of various citrus species in the family Rutaceae; it primarily refers to Citrus × sinensis, which is also called sweet orange, to distinguish it from the related Citrus × aurantium, referred to as bitter orange.
``` ```python import tkinter as tk from tkhtmlview import HTMLText, RenderHTML root = tk.Tk() html_label = HTMLText(root, html=RenderHTML('index.html')) html_label.pack(fill="both", expand=True) html_label.fit_height() root.mainloop() ``` -------------------------------- ### Method: set_html Source: https://github.com/bauripalash/tkhtmlview/blob/main/README.md Updates the content of the widget by parsing and rendering the provided HTML string. ```APIDOC ## set_html(html, strip=True) ### Description Sets the text content of the widget using HTML formatting. ### Parameters #### Arguments - **html** (string) - Required - The input HTML string to be rendered. - **strip** (boolean) - Optional - If True (default), handles spaces in HTML-like style. ``` -------------------------------- ### Method: fit_height Source: https://github.com/bauripalash/tkhtmlview/blob/main/README.md Adjusts the widget height to ensure all wrapped lines are visible. ```APIDOC ## fit_height() ### Description Calculates and sets the widget height to display all wrapped lines without clipping. ``` -------------------------------- ### Load HTML from External File using RenderHTML Source: https://context7.com/bauripalash/tkhtmlview/llms.txt Utilize the RenderHTML class to load HTML content from an external .html file. This allows for better separation of HTML content and Python code. The loaded HTML can then be used with HTML widgets, and the raw HTML string can be accessed using get_html(). ```python import tkinter as tk from tkhtmlview import HTMLText, RenderHTML # First, create an HTML file (content.html): # # # #This content is loaded from an external file.
#
#
#
root = tk.Tk()
root.title("RenderHTML Example")
# Load HTML from external file
rendered_html = RenderHTML('content.html')
# Use rendered HTML with widget
html_widget = HTMLText(root, html=rendered_html)
html_widget.pack(fill="both", expand=True)
html_widget.fit_height()
# You can also access the raw HTML string
print(rendered_html.get_html())
root.mainloop()
```
--------------------------------
### Class: RenderHTML
Source: https://github.com/bauripalash/tkhtmlview/blob/main/README.md
Utility class used to load and render HTML content from external files.
```APIDOC
## RenderHTML(file_path)
### Description
Loads an HTML file from the local filesystem to be used as input for widget rendering.
### Parameters
#### Arguments
- **file_path** (string) - Required - The path to the .html file to be rendered.
```
--------------------------------
### Render Preformatted Text and Code in Tkinter
Source: https://context7.com/bauripalash/tkhtmlview/llms.txt
Uses pre and code tags to display monospaced text and code snippets while preserving whitespace.
```python
import tkinter as tk
from tkhtmlview import HTMLScrolledText
root = tk.Tk()
root.title("Code Example")
root.geometry("600x500")
html_content = """
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
# Calculate first 10 numbers
for i in range(10):
print(fibonacci(i))
Use the print() function to output text.
Variables are declared with name = value syntax.
┌──────────┬──────────┐
│ Column1 │ Column2 │
├──────────┼──────────┤
│ Value1 │ Value2 │
└──────────┴──────────┘
"""
widget = HTMLScrolledText(root, html=html_content)
widget.pack(fill="both", expand=True, padx=10, pady=10)
root.mainloop()
```
--------------------------------
### Render Hyperlinks in Tkinter
Source: https://context7.com/bauripalash/tkhtmlview/llms.txt
Uses HTMLLabel to display clickable links that open in the default system browser. Links automatically inherit blue underline styling and cursor changes.
```python
import tkinter as tk
from tkhtmlview import HTMLLabel
root = tk.Tk()
root.title("Hyperlinks Example")
html_content = """
Check out these resources:
Links with custom styling:
Green styled linkVisited links change to purple color.
""" widget = HTMLLabel(root, html=html_content) widget.pack(fill="both", expand=True, padx=10, pady=10) widget.fit_height() root.mainloop() ``` -------------------------------- ### Create and Display HTMLText Source: https://context7.com/bauripalash/tkhtmlview/llms.txt Use HTMLText for displaying HTML content that fits within a fixed area without a scrollbar. It is editable by default. Call fit_height() to adjust the widget's height to its content. ```python import tkinter as tk from tkhtmlview import HTMLText root = tk.Tk() root.title("HTMLText Example") # Create HTML text widget with multiple formatted elements html_text = HTMLText(root, html="""This product features premium quality materials and innovative design.
Click a button to load content
') def load_content_a(): html_label.set_html("""This is the first content section.
""") html_label.fit_height() def load_content_b(): # strip=False preserves whitespace exactly as written html_label.set_html("""Preformatted text with spaces""", strip=True) html_label.fit_height() btn_frame = tk.Frame(root) btn_frame.pack(pady=10) tk.Button(btn_frame, text="Load A", command=load_content_a).pack(side=tk.LEFT, padx=5) tk.Button(btn_frame, text="Load B", command=load_content_b).pack(side=tk.LEFT, padx=5) root.mainloop() ``` -------------------------------- ### Automatically Adjust Widget Height with fit_height() Source: https://context7.com/bauripalash/tkhtmlview/llms.txt The fit_height() method adjusts the widget's height to fit its content, useful for dynamically changing content. It works for both short and long text, including wrapped lines. ```python import tkinter as tk from tkhtmlview import HTMLLabel root = tk.Tk() root.title("fit_height() Example") # Short content label1 = HTMLLabel(root, html='
Short text
') label1.pack(fill="x", padx=10, pady=5) label1.fit_height() # Adjusts to minimal height # Longer content label2 = HTMLLabel(root, html="""This widget contains more text that wraps across multiple lines when the window is narrow enough. The fit_height method calculates the required height to show all wrapped lines.
Regular paragraph with custom size and color.
Highlighted text with yellow background.
Custom font family textUnderlined red text
Strikethrough text
Loading image from URL:
Same image with different dimensions:
Load from local file path:
Images are cached after first load for better performance.
""" widget = HTMLScrolledText(root, html=html_content) widget.pack(fill="both", expand=True, padx=10, pady=10) root.mainloop() ``` -------------------------------- ### Render Tables in Tkinter Source: https://context7.com/bauripalash/tkhtmlview/llms.txt Displays tabular data using standard HTML table tags within an HTMLScrolledText widget. ```python import tkinter as tk from tkhtmlview import HTMLScrolledText root = tk.Tk() root.title("Table Example") root.geometry("500x400") html_content = """| Name | Age | City |
|---|---|---|
| Alice | 28 | New York |
| Bob | 34 | Los Angeles |
| Charlie | 42 | Chicago |
Table cells support text formatting tags.
""" widget = HTMLScrolledText(root, html=html_content) widget.pack(fill="both", expand=True, padx=10, pady=10) root.mainloop() ``` -------------------------------- ### Create and Display HTMLScrolledText Source: https://context7.com/bauripalash/tkhtmlview/llms.txt Use HTMLScrolledText for displaying longer HTML content that may exceed the visible area, providing built-in vertical scrolling and optional horizontal scrolling. It supports various HTML elements including headings, lists, preformatted text, and hyperlinks. ```python import tkinter as tk from tkhtmlview import HTMLScrolledText root = tk.Tk() root.title("HTMLScrolledText Example") root.geometry("800x600") # Create scrolled HTML widget with extensive content html_scrolled = HTMLScrolledText( root, html="""Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut quam sapien. Maecenas porta tempus mauris sed ullamcorper.
def hello_world():
print("Hello, World!")
Visit GitHub for more information.
"") html_scrolled.pack(fill="both", expand=True) root.mainloop() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.