Coder Social home page Coder Social logo

tkhtmlview's Introduction

tkhtmlview

PyPI Publish to Pypi

HTML widgets for tkinter

Fork of tk_html_widgets

Overview

This module is a collection of tkinter widgets whose text can be set in HTML format. A HTML widget isn't a web browser frame, it's only a simple and lightweight HTML parser that formats the tags used by the tkinter Text base class. The widgets behaviour is similar to the PyQt5 text widgets (see the PyQt5 HTML markup subset).

Installation

pip install tkhtmlview

Requirements

Example

import tkinter as tk
from tkhtmlview import HTMLLabel

root = tk.Tk()
html_label = HTMLLabel(root, html='<h1 style="color: red; text-align: center"> Hello World </H1>')
html_label.pack(fill="both", expand=True)
html_label.fit_height()
root.mainloop()

You can also save html in a separate .html file and then use RenderHTML to render html for widgets.

  • index.html

    <!DOCTYPE html>
    <html>
        <body>
            <h1>Orange is so Orange</h1>
            <img
            src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg"
            />
            <p>
            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.
            </p>
        </body>
    </html>
  • demo.py

    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()

Documentation

Classes

All widget classes inherits from the tkinter.Text() base class.

class HTMLScrolledText(tkinter.Text)

Text-box widget with vertical scrollbar

class HTMLText(tkinter.Text)

Text-box widget without vertical scrollbar

class HTMLLabel(tkinter.Text)

Text-box widget with label appearance

class RenderHTML

RenderHTML class will render HTML from .html file for the widgets.

Methods

def set_html(self, html, strip=True)

Description: Sets the text in HTML format.
> Args:

  • html: input HTML string
  • strip: if True (default) handles spaces in HTML-like style

def fit_height(self)

Description: Fit widget height in order to display all wrapped lines

HTML support

Only a subset of the whole HTML tags and attributes are supported (see table below). Where is possibile, I hope to add more HTML support in the next releases.

Tags Attributes Notes
a style, href
b style
br
code style
div style
em style
h1 style
h2 style
h3 style
h4 style
h5 style
h6 style
i style
img src, width, height experimental support for remote images
li style
mark style
ol style, type 1, a, A list types only
p style
pre style
span style
strong style
u style
ul style bullet glyphs only
table,tr,th,td - basic support

Note: All styles are not supported; align with justify is not supported; it falls back to left align

License

FOSSA Status

tkhtmlview's People

Contributors

artiebears13 avatar austinmatherne avatar bauripalash avatar fossabot avatar georgegebbett avatar hermfischer avatar marmidr avatar saadmairaj avatar veriables avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tkhtmlview's Issues

JavaScript in HTML

I want to use JavaScript in my html. Can I do it?
My html:

<html>
<head>
</head>
<body>
Your name: <input id="name" onblur="alert('Your name is '+document.getElementById('name').value);">
</body>
</html>

the span tag will automatically wrap.

First of all, thank you for your project, but I encountered a problem. When the number of words exceeds one line, the span tag will automatically wrap. I'm using a Windows 7 computer.

How to make a link open in the same document.

Hi and thanks for this piece of code:

I'm facing this problem, I want to use your code into another program to make an html help page for the program.

So I''ve done some text with the usual html tags:

 <ul>
  <li> <a href="#section2">Go to Section 2</a></li>

and the anchor text

<h3>CAM buttons</h3>
<a name="section2"> </a> 

But it open a new link in browser and not jump to the <a name=... tags

Best Regards

Carlo D.

Script, style and table tags not supported

Hi,
would it be possible to have an HTML table tag?
My HTML would look like this:

<!DOCTYPE html>
<html>
<body>
  <table id="myTable">
    <tr id="heads">
      <th>D</th>
      <th>a</th>
    </tr>
    <tr>
      <td>27/12/2020 08:52:55</td>
      <td>http://google.com</td>
    </tr>
    <tr>
      <td>27/12/2020 08:53:14</td>
      <td>http://evil.com</td>
    </tr>
    <tr>
      <td>27/12/2020 08:54:10</td>
      <td>http://google.com</td>
    </tr>
    <tr>
      <td>27/12/2020 08:54:34</td>
      <td>http://google.com</td>
    </tr>
  </table>
</body>
</html>

Thanks

Relative path to image files

Given the following directory structure:

script.py
help/feature.html
help/feature/images/diagram.png

If help/feature.html contains the following:

<html>
    Here is an image:

    <img src="images/diagram.png">
</html>

and script.py contains this:

from tkinter import Tk
from tkhtmlview import HTMLLabel, RenderHTML


root = Tk()

label = HTMLLabel(root, html=RenderHTML('help/feature.html'))
label.pack()

root.mainloop()

then I would have expected the image to be shown in the GUI. However, it only shows up if the src attribute in the in the img tag is changed to contain the absolute path to the PNG file.

Support for image file names that are relative to the HTML document would be useful and more consistent with how browsers handle it.

Package on PyPI not updated with recent typo fixes.

Hi there, love this package!

It seems the "BACKGROUD" typo fixes have not been pushed to the PyPI package yet, so installing the package via pip introduces errors when running.

If anyone is facing the same issue, until the package is updated, you can do the following on a Windows PC (not sure about other OS):

If you are using a virtual environment:

  • navigate to \env\Lib\site-packages\tkhtmlview in your project folder
  • open html_parser.py
  • edit lines 359, 360 replacing "BACKGROUD" with "BACKGROUND" and save the file

If you are not using a virtual environment:

  • navigate to ~your_user_folder\AppData\Local\Programs\Python~your_Python_version\Lib\site-packages\tkhtmlview
  • open html_parser.py
  • edit lines 359, 360 replacing "BACKGROUD" with "BACKGROUND" and save the file

Once the package has been updated feel free to close this issue.

Thanks again for the awesome work!

Issue pillow dependencies

Hello,

I'm having this issue:

Because no versions of tkhtmlview match >0.2.0,<0.3.0
and tkhtmlview (0.2.0) depends on Pillow (>=9.4.0,<10.0.0), tkhtmlview (>=0.2.0,<0.3.0) requires Pillow (>=9.4.0,<10.0.0).
So, because hank depends on both pillow (^10.2.0) and tkhtmlview (^0.2.0), version solving failed.

The problem is that I can't downgrade pillow because when I try to install pillow 9.5.0 I have issues with the wheel.

Could you please fix this.

Thanks

[TODO] Rewrite

The codebase being forked from another project, the project has too much complexity making it hard to maintain and improve.

Rewriting it from scratch but still making it backwards compatible is going to be difficult but I must do it in near future.

Support select and copy to clipboard

I am testing an HTMLScrolledText widget. I want to be able to select text so that I can copy it to the clipboard. But none of the standard selection methods work:

  1. Using mouse I am unable to click-drag to select anything. Instead wherever I click it places a cursor, and moving the mouse simply moves the cursor.
  2. Holding Shift-Arrow key also only moves cursor without highlighting.

Forgotten print statement

Hello an first thank you for sharing this source.

A small correction to upload on pipy please :
There is a forgotten "print" statement in html_parser.py , l495., adding clutter to the console...

White space?

Does this support using &nbsp to add white space?

PRE tag does not preserve spaces when combined with SPAN

Consider the following example:

<html>
<body>

<h1>The pre element</h1>
<pre>
C1     : <span style="color: Gray">BOM =</span>2u2/25/0603                           , <span style="color: Gray">PnP =</span>2u2_25V
C2     : <span style="color: Gray">BOM =</span><span style="color: IndianRed"></span>22pF<span style="color: IndianRed">/50/0603/COG                      </span>, <span style="color: Gray">PnP =</span>22pF
C3     : <span style="color: Gray">BOM =</span><span style="color: IndianRed"></span>22pF<span style="color: IndianRed">/50/0603/COG                      </span>, <span style="color: Gray">PnP =</span>22pF
R65    : <span style="color: Gray">BOM =</span>0603; 220Ω; 100mW; ±1%; 100ppm        , <span style="color: Gray">PnP =</span>220R
</pre>

</body>
</html>

PRE block itself preserves the spaces, but not if SPAN is used inside.
PnP = column shall be aligned, but it is not, since spaces are not preserved.

Check the expected output here:
https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_pre

ModuleNotFoundError while installing

I had tkhtmlview as a dependency for my application (fishy), some people are getting ModuleNotFoundError for requests and PIL while installing using pip

for now, I just removed your library as a dependency and manually copied your code in my codebase, which I don't mind, just thought u should know that there is this issue

the strange thing is, i wasn't able to reproduce the issue, but multiple people were getting the same issue.

How to reproduce?

  1. install new python 3.7.3
  2. pip install fishy==0.3.3

Workaround:

  1. run pip install Pillow requests before installing fishy

Request for Dependency Upgrade: Pillow >=10.0.1

Description:

I would like to request an upgrade to the Pillow dependency in tkhtmlview. Currently, tkhtmlview relies on Pillow version >=9.4.0,<10.0.0, which includes a bundled libwebp binary that is known to be vulnerable to CVE-2023-5129 (previously CVE-2023-4863).

Suggested Action:

I propose upgrading the Pillow dependency to version >=10.0.1 or a higher version. Pillow v10.0.1 includes an updated libwebp binary to v1.3.2, which resolves the security vulnerability.

Rationale:

Addressing this security vulnerability is crucial to ensure the safety and reliability of tkhtmlview and its users. Upgrading the dependency will contribute to the overall security of the project and the broader Python ecosystem.

Additional Information:

  • Package Name: tkhtmlview
  • Current Version: 0.2.0
  • Python Version: >=3.7,<4.0

Thank you for considering this request, and I appreciate your efforts in maintaining the tkhtmlview project.

[KNOWN ISSUE] Styles, Headers, bold almost nothing works in Anaconda Distributions

Anaconda Distribution's Tkinter package has some issues regarding Tkinter package (ContinuumIO/anaconda-issues#6833), which they don't intend to fix.

Their reply to the issue I meantioned above: ContinuumIO/anaconda-issues#6833 (comment)

No we cannot do this. When building our software we need python built very early, well before anything graphical gets built. Adding Freetype as a dep for tkinter causes a cycle in the build graph and we can no longer build the distro.
Why not use something more modern than tkinter anyway?

For that reasons, most features of tkhtmlview will not work in Anaconda distributions including mambaforge, miniconda etc.

With Anaconda's Python:

Screenshot_20220908_092447

Without Anaconda (system python):

Screenshot_20220908_092422


This is not a bug of the package tkhtmlview

Tkinter tkhtmlview Package

I am not able to see any color nor i am able to set color for select feature.

If a user selects any word or letter in html, i am able to get that word by selection_get() method, but i am not able to see or set color i.e. by default blue.

Another Problem is not able apply style which is put in head tag. Taking as a string and printing it.

code.txt

How to tackle it.
code.txt

TODO: Unit Tests

Writing tests for tkinter applications seems very complicated and under-documented.
Need to find a way to do that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.