[Tkinter] manipulation of string in Text widget - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] manipulation of string in Text widget (/thread-24464.html) |
manipulation of string in Text widget - Stauricus - Feb-15-2020 hello everybody I'm trying to create a simple text editor in Tkinter, to help building HTML pages. I just write things in the text area, and use buttons to add tags, like <h1>. this is what I have so far: from tkinter import * root = Tk() def addHeader(): a = main_text.get(Tk.SEL_FIRST, Tk.SEL_LAST) main_text = Text(root).grid(row=0, column=0, rowspan=6) b_header = Button(root, text ="Header", command=addHeader).grid(row=0, column=1, padx=2, pady=2) spin_header = Spinbox(root, from_=1, to=6, width=5).grid(row=0, column=2, padx=2, pady=2) b_ref = Button(root, text ="Referência").grid(row=1, column=1, padx=2, pady=2) spin_href = Spinbox(root, from_=0, to=9999, width=5).grid(row=1, column=2, padx=2, pady=2) name_label = Label(root, text="Nome do Arquivo: ").grid(row=2, column=1, columnspan=2, padx=2, pady=2) name_entry = Entry(root).grid(row=3, column=1, columnspan=2, padx=2, pady=2) b_save = Button(root, text ="Finalizar e Salvar").grid(row=4, column=1, columnspan=2, padx=2, pady=2) root.mainloop()what I want to achieve is, when the b_header is pressed, html tags are added around whatever is selected in the text area (ie, add <h1> at the beggining of the selected text and </h1> at the end). if nothing is selected, just add the tags in the cursor position. but currently I get this error when I press the button (the function addHeader is not working): any ideas on how to make it work?thanks in advance RE: manipulation of string in Text widget - Larz60+ - Feb-15-2020 This book: https://github.com/manash-biswal/Python-Books/blob/master/Tkinter%20GUI%20Application%20Development%20Hotshot.pdf Has the best coverage of text widget manipulation I've ever seen. Worth taking look, **NOTE** I'd suggest purchasing a copy Here: https://www.packtpub.com/application-development/tkinter-gui-application-development-hotshot or here: https://www.amazon.com/Tkinter-GUI-Application-Development-HOTSHOT-ebook/dp/B00G8YAUX4 The one on GitHub may be copyright infringement RE: manipulation of string in Text widget - Stauricus - Feb-17-2020 thanks, I found the answer! the problem was this line: b_header = Button(root, text ="Header", command=addHeader).grid(row=0, column=1, padx=2, pady=2)doing it in a single line made the b_header return 'NoneType'. I just had to change it to: b_header = Button(root, text ="Header", command=addHeader) b_header.grid(row=0, column=1, padx=2, pady=2)and of course I changed a lot of things. here is what I have so far, if it interests anyone. it's almost finished, to be honest. from tkinter import * from tkinter import filedialog import re root = Tk() def addHeader(): if main_text.tag_ranges("sel"): t = main_text.get("sel.first", "sel.last") main_text.delete("sel.first", "sel.last") else: t = "" t = "<h" + spin_header.get() + ">" + t + "</h" + spin_header.get() + ">" main_text.insert(INSERT, t) def addParagraph(): if main_text.tag_ranges("sel"): t = main_text.get("sel.first", "sel.last") main_text.delete("sel.first", "sel.last") else: t = "" t = "<p>" + t + "</p>" main_text.insert(INSERT, t) def addPeriod(): if main_text.tag_ranges("sel"): t = main_text.get("sel.first", "sel.last") main_text.delete("sel.first", "sel.last") else: t = "" t = t + "<sup>[" + spin_period.get() + "]</sup>" main_text.insert(INSERT, t) def addOL(): if main_text.tag_ranges("sel"): t = main_text.get("sel.first", "sel.last") main_text.delete("sel.first", "sel.last") else: t = "" t = "<ol>\n" + t + "\n</ol>" main_text.insert(INSERT, t) def addUL(): if main_text.tag_ranges("sel"): t = main_text.get("sel.first", "sel.last") main_text.delete("sel.first", "sel.last") else: t = "" t = "<ul>\n" + t + "\n</ul>" main_text.insert(INSERT, t) def addLI(): if main_text.tag_ranges("sel"): t = main_text.get("sel.first", "sel.last") main_text.delete("sel.first", "sel.last") else: t = "" t = "<li>" + t + "</li>" main_text.insert(INSERT, t) def addReference(): filename = filedialog.askopenfilename(initialdir = "../").replace(" ", "%20") main_text.insert(INSERT, "<a href=\"" + filename + "#page=3\">Nome</a>") def finalizeArticle(): t = """<!DOCTYPE html> <html lang="pt-br"> <head> <title>""" + name_entry.get() + """</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <h1>""" + name_entry.get() + """</h1> <div class="menu">\n""" headers2 = re.findall(re.compile("<h2>(.+?)</h2>"), main_text.get("1.0",END)) headers2_links = [] for h in headers2: headers2_links.append(h.replace(" ", "_")) t = t + "\t<a href=#" + h.replace(" ", "_") + ">" + h + "</a>\n" t = t + "<a href=#Referências>Referências</a>\n</div>\n\n" + main_text.get("1.0", END) + """<br><br> </body> </html> """ for i in range(len(headers2)): t = t.replace("<h2>" + headers2[i], "<h2 id=" + headers2_links[i] + ">" + headers2[i]) file = open(name_entry.get().lower().replace(" ", "_") + ".html", "w") file.write(t) file.close() main_text = Text(root)#, width=140, height=40) main_text.grid(row=0, column=0, rowspan=8) main_text.insert("1.0", "\n\n<h2 id=Referências>Referências</h2>\n<ol>\n\n</ol>") b_header = Button(root, text ="Header", command=addHeader) b_header.grid(row=0, column=1, padx=2, pady=2) spin_header = Spinbox(root, from_=1, to=6, width=5, textvariable=DoubleVar(value=2)) spin_header.grid(row=0, column=2, padx=2, pady=2) b_paragr = Button(root, text ="Parágrafo", command=addParagraph) b_paragr.grid(row=1, column=1, columnspan=2, padx=2, pady=2) b_period = Button(root, text ="Frase", command=addPeriod) b_period.grid(row=2, column=1, padx=2, pady=2) spin_period = Spinbox(root, from_=0, to=9999, width=5) spin_period.grid(row=2, column=2, padx=2, pady=2) b_ol = Button(root, text ="<ol>", command=addOL) b_ol.grid(row=3, column=1, padx=2, pady=2) b_ul = Button(root, text ="<ul>", command=addUL) b_ul.grid(row=3, column=2, padx=2, pady=2) b_li = Button(root, text ="<li>", command=addLI) b_li.grid(row=4, column=1, columnspan=2, padx=2, pady=2) b_ref = Button(root, text ="Adicionar Referência", command=addReference) b_ref.grid(row=5, column=1, columnspan=2, padx=2, pady=2) name_label = Label(root, text="Nome do Artigo: ").grid(row=6, column=1, columnspan=2, padx=2, pady=2) name_entry = Entry(root) name_entry.grid(row=7, column=1, columnspan=2, padx=2, pady=2) b_save = Button(root, text ="Finalizar e Salvar", command=finalizeArticle) b_save.grid(row=8, column=1, columnspan=2, padx=2, pady=2) root.mainloop() |