Als Vorlage für eine einfache Hilfeseite hatte ich versucht in einem Textfeld eine formatierte Ausgabe zu erreichen, wobei ich zuerst ein einfaches HTML-Grundgerüst für eine HTML-Quellseite benutzte und diesen HTMLParser.
https://docs.python.org/3/library/html.parser.html
Letztendlich kam ich nicht wirklich weiter, weil ich es nicht schaffte, den Tag von "def handle_starttag" und data von "def handle_data" zusammen in einem Dictionary zu speichern, um die Zuordnung zu erhalten. Vielleicht habe ich mich nur zu dumm angestellt.
Mit einer darauf zugeschnittenen XML-Datei als Quelle hat es dann geklappt, ist aber sicherlich noch verbesserungswürdig.
[codebox=xml file=Unbenannt.xml]<?xml version="1.0" encoding="utf-8"?>
<hilfe>
<einleitung>
<h1>Titel</h1>
<h2>Untertitel</h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum.(br)
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor
sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed
diam voluptua.</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr...</p>
</einleitung>
<punkt>
<h3>Punkt 1</h3>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr...1</p>
<img>testbild.png</img>
<notiz>Notizen unterm Bild</notiz>
</punkt>
<punkt>
<h3>Punkt 2</h3>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr...2</p>
<p>Umlaute ÄÖÜäöüß</p>
</punkt>
</hilfe>[/code]
Code: Alles auswählen
from tkinter import Tk, Text, Scrollbar, PhotoImage, END
from PIL import Image, ImageTk
import xml.etree.ElementTree as ET
class Lesetest:
def __init__(self):
self.fenster = Tk()
self.textfeld = None
def layout(self):
self.fenster.title("Lesetest")
self.fenster.config(bg="#d9cda3")
self.textfeld = Text(
self.fenster, height=42, pady=12, padx=12, wrap="word")
scrollbar = Scrollbar(self.fenster)
scrollbar.config(command = self.textfeld.yview)
self.textfeld.config(yscrollcommand = scrollbar.set)
scrollbar.pack(side="right", fill="y")
self.textfeld.pack(pady=24, padx=24)
self.textfeld.tag_config("h1", font=("verdana", 18, "bold"))
self.textfeld.tag_config("h2", font=("verdana", 12, "bold"))
self.textfeld.tag_config("h3", font=("verdana", 10, "bold"))
self.textfeld.tag_config("p", font=("verdana", 10))
self.textfeld.tag_config("img", justify="center")
self.textfeld.tag_config(
"notiz", font=("verdana", 9), foreground="#867a44", justify="center")
self.textfeld.tag_config(
"fehler", font=("verdana", 10), foreground="#ff8808")
def parse_xml(self):
try:
tree = ET.parse("lorem-ipsum.xml")
root = tree.getroot()
except FileNotFoundError:
root = ET.fromstring("<root><hinweis><fehler>"
"Die Datei wurde nicht gefunden!"
"</fehler></hinweis></root>")
for n in range(len(root)):
for child in root[n]:
if child.text:
content = child.text.replace("\n", "").replace(
"\x20\x20", "").replace("(br)", "\n")
else:
content = ""
if child.tag == "h1":
self.textfeld.insert(1.0, "{}\n".format(content), "h1")
if child.tag == "h2":
self.textfeld.insert(END, "{}\n\n".format(content), "h2")
if child.tag == "h3":
self.textfeld.insert(END, "{}\n\n".format(content), "h3")
if child.tag == "p":
self.textfeld.insert(END, "{}\n\n".format(content), "p")
# Vor und hinter dem Image ein Leerzeichen einfügen, um es mit
# den Leerzeichen in Reihe mittig mit dem "img"-Tag auszurichten.
if child.tag == "img":
try:
image = Image.open("testbild.png")
self.testbild = ImageTk.PhotoImage(image)
self.textfeld.insert(END, "\x20", "img")
self.textfeld.image_create(END, image = self.testbild)
self.textfeld.insert(END, "\x20\n")
except FileNotFoundError:
self.textfeld.insert(
END, "Das Image wurde nicht gefunden.\n\n", "fehler")
if child.tag == "notiz":
self.textfeld.insert(END, "{}\n\n".format(content), "notiz")
if child.tag == "fehler":
self.textfeld.insert(END, "{}\n\n".format(content), "fehler")
def main(self):
self.layout()
self.parse_xml()
self.fenster.mainloop()
if __name__ == "__main__":
Lesetest().main()