Ich habe eine kleine mini App mit 2 Text-Widgets erstellt die
ausgewählten Text in beiden Textfeldern unabhängig voneinander
in "fett/unterstreichen/kursiv" formatieren kann.
Nun möchte ich, dass der formatierte Text genau so, in einer Datei wie
.txt / .json oder .html gespeichert wird.
Und per Knopfdruck genau so wieder im ursprünglichen Text-Format in die
Text-Widgets zurück geladen werden kann.
Den formatierten Text mittels json.dump zu speichern funktioniert soweit.
Das Problem dabei ist allerdings beim Laden, da der Text immer nur im .json-Format
zurück geladen wird?!
Also statt ein Text-Format wie ( Test, eins - zwei - drei ) beim Laden zu erhalten,
erscheint im Textwidget:
Code: Alles auswählen
[["text", "Test, ", "1.0"], ["tagon", "bold", "1.6"], ["text", "eins", "1.6"], ["tagoff", "bold", "1.10"], ["text", " - ", "1.10"], ["tagon", "underline", "1.13"], ["text", "zwei", "1.13"], ["tagoff", "underline", "1.17"], ["text", " - ", "1.17"], ["tagon", "italic", "1.20"], ["text", "drei", "1.20"]]
PyQt5 ist allerdings eine Welt, mit der ich mich kaum auskenne.
Kennt ihr einen Weg die Texte formatiert wieder zurück zu laden?
PS: Ich habe im Internet gelesen, dass es wohl leichter ist, das Ganze über html-Format abzuwickeln, statt über .json oder Text.
Ich würde mich wirklich absolut über Eure Hilfe freuen.
Hier der bisherige Code:
Code: Alles auswählen
import tkinter as tk
from tkinter import font
import json
class Gui():
def __init__(self, root):
root.geometry("500x600")
root.title("Bold and Unbold")
self.selected_field = None
#textfield_1
self.textfield_1 = tk.Text(root, width=20, height=10, wrap=tk.WORD,font=("Levetica", 12, 'normal'))
self.textfield_1.bind('<<Selection>>', self.select_field)
self.textfield_1.place(x=29,y=50,width=245,height=200)
#textfield_2
self.textfield_2 = tk.Text(root, width=20, height=10, wrap=tk.WORD,font=("Levetica",12))
self.textfield_2.bind('<<Selection>>', self.select_field)
self.textfield_2.place(x=29,y=300,width=245,height=200)
#button_1 - bold
self.ButtonMakeBold = tk.Button(root, text="fett", font=("Levetica", "9", "bold"),
command=self.make_bold)
self.ButtonMakeBold.place(x=28, y=5, width=40, height=40)
#button_2 - unbold
self.ButtonMakeUnbold = tk.Button(root, text="normal", font=("Levetica", "9", "normal"),
command=self.make_unbold)
self.ButtonMakeUnbold.place(x=75, y=5, width=50, height=40)
#button_3 - underline
self.ButtonMakeUnbold = tk.Button(root, text="unterstreichen", font=("Levetica", "9", "underline"),
command=self.make_underline)
self.ButtonMakeUnbold.place(x=130, y=5, width=90, height=40)
#button_4 - italic
self.ButtonMakeItalic = tk.Button(root, text="kursiv", font=("Levetica", "9", "italic"),
command=self.make_italic)
self.ButtonMakeItalic.place(x=225, y=5, width=50, height=40)
#button_5 - save textfields (in json format)
self.ButtonSave = tk.Button(root, text="als .json speichern", font=("Levetica", "9", "normal"),
command=self.save3)
self.ButtonSave.place(x=28, y=520, width=120, height=40)
#button_6 - load textfields (in json format)
self.ButtonLoad = tk.Button(root, text="von .json laden", font=("Levetica", "9", "normal"),
command=self.load3)
self.ButtonLoad.place(x=155, y=520, width=100, height=40)
#button_7 - save textfields (in html format)
self.ButtonSave = tk.Button(root, text="als html speichern", font=("Levetica", "9", "normal"),
command=lambda:[self.save1(), self.save2()])
self.ButtonSave.place(x=260, y=520, width=110, height=40)
#button_8 - load textfields (in html format)
self.ButtonLoad = tk.Button(root, text="als html laden", font=("Levetica", "9", "normal"),
command=lambda:[self.load1(), self.load2()])
self.ButtonLoad.place(x=375, y=520, width=100, height=40)
#bold the selected text in the textfield
def make_bold(self):
field = self.selected_field
field.tag_remove("normal", "sel.first", "sel.last")
field.tag_remove("underline", "sel.first", "sel.last")
field.tag_remove("italic", "sel.first", "sel.last")
field.tag_add("bold", "sel.first", "sel.last")
field.tag_config("bold", font=("Levetica", 12, "bold"))
#unbold the selected text in the textfield
def make_unbold(self):
field = self.selected_field
field.tag_remove("underline", "sel.first", "sel.last")
field.tag_remove("bold", "sel.first", "sel.last")
field.tag_remove("italic", "sel.first", "sel.last")
field.tag_add("normal", "sel.first", "sel.last")
field.tag_config("normal", font=("Levetica", "12", "normal"))
#underline the selected text in the textfield
def make_underline(self):
field = self.selected_field
field.tag_remove("normal", "sel.first", "sel.last")
field.tag_remove("bold", "sel.first", "sel.last")
field.tag_remove("italic", "sel.first", "sel.last")
field.tag_add("underline", "sel.first", "sel.last")
field.tag_config("underline", font=("Levetica", 12, "underline"))
#italic the selected text in the textfield
def make_italic(self):
field = self.selected_field
field.tag_remove("normal", "sel.first", "sel.last")
field.tag_remove("bold", "sel.first", "sel.last")
field.tag_remove("underline", "sel.first", "sel.last")
field.tag_add("italic", "sel.first", "sel.last")
field.tag_config("italic", font=("Levetica", 12, "italic"))
#select the text in the textfield
def select_field(self, event):
self.selected_field = event.widget #event.widget is the text widget that received the event
#convert the text in the textfield_1 to a html file fomat
def save1(self):
text = self.textfield_1.get("1.0", "end-1c")
f = open("text1.html", "w")
f.write(text)
f.close()
print("saved")
#convert the text in the textfield_2 to a html file fomat
def save2(self):
text = self.textfield_2.get("1.0", "end-1c")
f = open("text2.html", "w")
f.write(text)
f.close()
print("saved")
#save the text in the textfield_1 and textfield_2 to json files with dump and utf-8
def save3(self):
with open("text1.json", "w", encoding="utf-8") as f:
json.dump(self.textfield_1.dump("1.0", "end-1c"), f)
with open("text2.json", "w", encoding="utf-8") as f:
json.dump(self.textfield_2.dump("1.0", "end-1c"), f)
print("saved")
#load the text from the html file to the textfield_1
def load1(self):
f = open("text1.html", "r")
text = f.read()
self.textfield_1.insert("1.0", text)
f.close()
print("loaded")
#load the text from the html file to the textfield_2
def load2(self):
f = open("text2.html", "r")
text = f.read()
self.textfield_2.insert("1.0", text)
f.close()
print("loaded")
#load the text from the json files to the textfield_1 and textfield_2 with load and utf-8
def load3(self):
with open("text1.json", "r", encoding="utf-8") as f:
text = json.load(f)
self.textfield_1.insert("1.0", text)
with open("text2.json", "r", encoding="utf-8") as f:
text = json.load(f)
self.textfield_2.insert("1.0", text)
print("loaded")
def main():
root = tk.Tk()
Gui(root)
root.mainloop()
if __name__ == '__main__':
main()
Liebe Grüße,
Marlon