Code: Select all
Internal Links (#id): When I click an internal link (e.g., [url=#introduction]Introduction[/url]), it's treated like a normal URL (e.g., file:///C:/path/#introduction), which causes issues in scrolling to the section.
External Links (http://): External URLs open correctly in a web browser, but internal links are not being processed to scroll to the section they point to within the HtmlFrame.
< /code>
Ich verwende die Funktion open_link (URL), um alle Link -Klicks zu verarbeiten. Mein aktueller Ansatz zum Erkennen und Verhalten interner und externer Links besteht darin, WebBrowser.open () für externe Links zu verwenden, aber interne Links (#ID) verhalten sich nicht wie erwartet - sie werden als Datei -URLs behandelt, anstatt in den entsprechenden Abschnitt innerhalb des HTMLFrame zu navigieren.When an internal link (e.g., #introduction) is clicked, the viewer should scroll to the appropriate section inside the HtmlFrame without opening a new URL or file.
When an external URL (e.g., https://www.google.com) is clicked, it should open in the default web browser.
def open_link(url):
if url.startswith("#"):
html_frame.load_html(html_content) # Reload to keep content
html_frame.tk.call("eval", f "document.getElementById ('{url [1:]}'). scrollintoview ();")#scrollen />html_frame.load_html(html_content)
html_frame.tk.call("eval ", f" document.getElementById ('{Section_id}'). Scrollintoview (); Probleme mit internen Links, die als Datei -URLs behandelt werden, und die Scroll -Funktionalität scheint nicht zu funktionieren.
Was ich ausprobiert habe: < /p>
Code: Select all
Used html_frame.load_html(html_content) to reload the page and then scroll to the element.
Attempted using tk.call("eval", f"document.getElementById('{section_id}').scrollIntoView();") to trigger scrolling, but this doesn't seem to work with tkinterweb.
How can I properly handle internal anchor links (#id) in tkinterweb.HtmlFrame to scroll to the section instead of treating it as a file URL?
Is there a way to differentiate between internal links and external URLs and handle them correctly in tkinterweb?