Andereoo / TkinterWeb

Python bindings for Tkhtml.
MIT License
146 stars 16 forks source link

css not working #71

Closed oberluz closed 1 year ago

oberluz commented 1 year ago

Hi,

I loaded the styles.css generated by pygmentize using the html_frame.add_css method and then called html_frame.load_html with some html code that uses the styles but they are not rendered in colour.

The only way I can get it to render them is to call html_frame.load_html(' + html) and this works also without calling html_frame.add_css.

am I using html_frame.add_css incorrectly?

oberluz commented 1 year ago

i just tried reversing the calls and it works fine in this order:

html_frame.load_html(html)
html_frame.add_css(css)

Maybe this should be stated in the docs...

Andereoo commented 1 year ago

Hi! load_html clears the current page, including CSS. If you want to set the CSS before adding the HTML, use add_html instead. Good idea - I'll tweak the docs to make that more clear.

oberluz commented 1 year ago

makes sense now. thanks!

oberluz commented 1 year ago

makes sense now. Great project BTW, thanks!

Andereoo commented 1 year ago

No worries! Let me know if you have any other questions!

oberluz commented 1 year ago

Not sure if I've found a bug but if I do:

self.description_text.html.reset()  # I need a reset so that the new content is not appended to the previous content
self.description_text.add_css(self.css)
self.description_text.add_html(html)

instead of

self.description_text.add_html(html)
self.description_text.add_css(self.css)

the images in the html are not displayed

Andereoo commented 1 year ago

Could you give me some sample code?

oberluz commented 1 year ago

ok, I'll craft a small example

oberluz commented 1 year ago

Run the following with an image.png in the same directory

from tkinterweb import HtmlFrame
from tkinter import Tk

class Window(Tk):
    def __init__(self):
        super().__init__()
        self.frame = HtmlFrame(self, height='5', messages_enabled=False)
        html = """
<h1>html with image</h1>
<p><img alt="" src="image.png" /></p>
<p>some text</p>
        """
        # works
        self.frame.load_html(html)
        self.frame.pack()

        # does not work
        # self.frame.html.reset() 
        # self.frame.add_html(html)
        # self.frame.pack()

def main():
    window = Window()
    window.mainloop()

if __name__ == '__main__':
    main()  # pragma: no cover

still breaks even without the call to reset

oberluz commented 1 year ago

I raised https://github.com/Andereoo/TkinterWeb/issues/72 to deal with this as a separate issue as this issue is closed anyway.