Python Forum
[Tkinter] manipulation of string in Text widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] manipulation of string in Text widget
#1
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>.
[Image: Captura-de-tela-de-2020-02-15-11-44-44.png]
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):
Error:
Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__ return self.func(*args) File "teste.py", line 5, in addHeader a = main_text.get(Tk.SEL_FIRST, Tk.SEL_LAST) AttributeError: 'NoneType' object has no attribute 'get'
any ideas on how to make it work?
thanks in advance Big Grin
Reply


Messages In This Thread
manipulation of string in Text widget - by Stauricus - Feb-15-2020, 02:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: could not convert string to float: '' fron Entry Widget russellm44 5 727 Mar-06-2024, 08:42 PM
Last Post: russellm44
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,960 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Text widget inert mode on and off rfresh737 5 3,910 Apr-19-2021, 02:18 PM
Last Post: joe_momma
  Line numbers in Text widget rfresh737 3 5,453 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  tkinter text widget word wrap position chrisdb 6 7,605 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,437 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to place global tk text widget in class or on canvas puje 1 2,346 Jul-04-2020, 09:25 AM
Last Post: deanhystad
  [Tkinter] Paste Operation not working in Text Widget Code_Enthusiast 1 2,975 Sep-11-2019, 08:49 PM
Last Post: Larz60+
  how to insert image into Text widget Tkinter atlass218 5 10,055 Apr-17-2019, 05:28 AM
Last Post: atlass218
  [Tkinter] Lock Text Widget height Oxylium 3 4,242 Feb-14-2019, 10:13 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020