napisz kod który w zależności od html tag wyświetla odpowiedni tekst w textbox tkinter…
- User
napisz kod który w zależności od html tag wyświetla odpowiedni tekst w textbox tkinter tutaj przykład funkcji dla
def em_points(text): suppat = re.compile(r'\w*') suppatiter = suppat.findall(text) if suppatiter: for suptag in suppatiter: text = "".join(text.split(suptag)) finds = list() if "" in text: find_points = list() emcount = text.count("") for _ in range(emcount): find_open = text.find("") text = text[:find_open] + text[find_open + 4:] find_close = text.find("") text = text[:find_close] + text[find_close + 5:] find_points.append([find_open, find_close]) for points in find_points: finds.append(text[points[0]: points[1]]) return [text, finds]
def italicize_text(text_box, finds): italics_font = font.Font(text_box, text_box.cget("font")) italics_font.configure(slant="italic") text_box.tag_configure("italics", font=italics_font) text_in_box = text_box.get(1.0, END) used_points = list() for find in finds: if find not in text_in_box: raise RuntimeError(f"Could not find text to italicise in textbox:\n {find}\n {text_in_box}") else: start_point = text_in_box.find(find) end_point = start_point + len(find) found_at = [start_point, end_point] if found_at in used_points: while found_at in used_points: reduced_text = text_in_box[end_point:] start_point = end_point + reduced_text.find(find) end_point = start_point + len(find) found_at = [start_point, end_point] used_points.append(found_at) text_to_startpoint = text_in_box[:start_point] text_to_endpoint = text_in_box[:end_point] start_line = text_to_startpoint.count("\n") + 1 end_line = text_to_endpoint.count("\n") + 1 if "\n" in text_to_startpoint: line_start_point = len(text_in_box[text_to_startpoint.rfind("\n") + 1: start_point]) else: line_start_point = start_point if "\n" in text_to_endpoint: line_end_point = len(text_in_box[text_to_endpoint.rfind("\n") + 1: end_point]) else: line_end_point = end_point start_point = Decimal(f"{start_line}.{line_start_point}") end_point = Decimal(f"{end_line}.{line_end_point}") text_box.tag_add("italics", start_point, end_point)
text = 'abcd efg hijk\nlmn"op qrs tuv wx yz'
em_text = em_points(text) clean_text = em_text[0] em_list = em_text[1]
print(clean_text) print(em_list)
text_box = Text(root, width=200, height=200, font=("Courier", 12)) text_box.grid(row=0,column=0,columnspan=2)
text_box.insert(1.0, clean_text) italicize_text(text_box, em_list)
root.mainloop()
- Assistant
- User
add to this code more implementation for diffrent html tag
- Assistant
- User
add to this code more implementation for all html tags
- Assistant
It would be impractical to include support for all HTML tags within the tkinter Text widget, as the Text widget has its limitations, and not all HTML tags have equivalent representations in tkinter. Furthermore, there are hundreds of HTML tags, and many of them are used for a variety of purposes, not just formatting text.
However, if you're looking to display HTML content within a Python application, you can use a library like PyQt5 or PyQt6. PyQt provides a
QWebEngineViewwidget that allows you to display HTML content within a PyQt window.Here's an example of how you can create a PyQt application that displays HTML content within a widget: