so I've got this little Text widget with a scroll bar and I've got a question. How do I make text in this Text widget a variable ? If I made this text a variable I would be able to open a text file and edit it's text or save the text I've written, etc or maybe it's a wrong way that I'm approaching this, is there a better way to do this ?
There is no option to associate a variable with a text widget. You can achieve the same thing by using variable traces and widget bindings but it's rarely worth the effort.
The typical way to interact with the text widget is to read a file into a variable then use the insert method of the widget to put the text into the widget. Then, to save you just do the reverse -- get the text from the widget with the get method, and write the data to a file.
One tip: when you do a get, don't get the text from 1.0 to "end", use "end-1c" instead. If you specify "end" as the last character you'll get the implicit newline that tk always adds, meaning your text file will grow by one character each time you do a load/save cycle.
Related
I am writing a python code to create a RICH TEXT EDITOR in pythonmy python text editor. It has a bold button and all formating buttons like italic,fonts etc.
These buttons would format the selected text.
But the problem with all these buttons is that First when I bold the selected text, it is working fine but at the second time it applies the same format to the previously selected text
This is my code for the bold and italic function :
def boldtxt():
bldbtn['activebackground']="#ffad33"
contboxfont['weight']="bold"
contbox.tag_add("bold",SEL_FIRST,SEL_LAST)
contbox.tag_config("bold",font=contboxfont)
def italictxt():
itlbtn['activebackground']="#ffad33"
contboxfont['slant']="italic"
contbox.tag_add("italic",SEL_FIRST,SEL_LAST)
contbox.tag_config("italic",font=contboxfont)
contbox=Text()
contbox.pack()
bldbtn=Button(comand=boldtxt)
bldbtn.pack()
itlbtn=Button(command=italictxt)
itlbtn.pack()
I want that every word (or selected item should be formatted according to the user.
For ex.
if the bold button is pressed the selected text should be bold
and if the italic button is pressed then the selected text would be italic.
Thanks in advance :)
If you want each word to be uniquely formatted, they each need to have a unique tag. In your code, everything uses the "bold" tag, so if you change the "bold" tag, all of the words would change. If you don't want that, each word will need its own unique tag.
So I have some tags (strings the user made), they are on display in a text widget at all times. Basically if there are tags to display, you need to be able to triple click on them and it will lead to a editing menu. But if there aren't any tags to display, I don't want people to be able to triple click on it.
So my thought was bind triple click to the appropriate function if there are tags to display, and unbind it if there aren't any tags to display.
for tag in sorted(tags_pre_listed):#This loop will just check the tags and OK them for use.
if tag[0:4]=='TAG-' and tag not in used_tags: # Just avoids duplicates.
tags_display_box.insert(Tk.END, '#'+tag[4:]+' ') #inserts the tag to the display.
used_tags.append(tag)
if len(used_tags)>0: #If any tags were used to display, it will bind Triple click.
tags_display_box.bind("<Triple-1>", delete_tag)
else: #This is where it tries to unbind if there are no tags, but fails.
tags_display_box.unbind('<Button-1>',"<Triple-1>")
The issue I get is
TclError: can't delete Tcl command
Sorry It may be a rookie answer for all I know but I have done my research and can't find a way around it at all T-T
Thanks so much for reading and for any advice!
The line:
tags_display_box.unbind('<Button 1>',"<Triple-1>")
should read:
tags_display_box.unbind('<Triple-1>')
As it is you're trying to unbind something that's not bound from a command that does not exist.
According to http://www.tkdocs.com/tutorial/text.html#more, in Tk/Tcl it is possible to embed "elided" text in a Text widget, text that is not displayed. This sounds useful. Is this functionality available in Python? If so, what is the API?
Below example produces a Text widget that has elided text in it, using tags:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.tag_config('mytag', elide=True)
text.insert('end', "This text is non-elided.")
text.tag_add('mytag', '1.13', '1.17')
def toggle_elision():
# cget returns string "1" or "0"
if int(text.tag_cget('mytag', 'elide')):
text.tag_config('mytag', elide=False)
else:
text.tag_config('mytag', elide=True)
tk.Button(root, text="Toggle", command=toggle_elision).pack()
root.mainloop()
Furas is quite right... The solution is as simple as the elide=True keyword arg passed to the tag_config() method. It's strange that the elide keyword is not documented in any Tkinter docs I can find. But, the simplest scenario is to create a tag config as follows:
textWidget.tag_config('hidden', elide=True) # or elide=1
This will cause the tagged text to be "invisible" or "hidden" in the text widget. You will not be able to see the text in the Text widget, but it is still there. If you call textWidget.get('1.0', 'end - 1c'), you'll see the hidden characters in the text returned by the method. You can also delete the hidden characters from textWidget without needing to see them. As you're deleting the elided characters, you won't see the INSERT cursor move. It's a bit odd...
Note that the tagged text can span multiple lines, so all lines are collapsed in the Text widget. The first thing I thought of while testing this was that, if I were implementing a source code editor and wanted to add the feature of "collapsing" part of the code (say, in an if block), elided text would be the feature I would want to be using to do that.
Thanks, Furas!
I'm using gtk treeview in one of my applications.
Application works as follows:
I extract some data from logfile and add the important data to my splite database and then, I show the data in treeview by fetching the rows from the database.
Now my question is how can I add newline character, so that I can add multi-lines in the treeview cellrenderer.
I tried this by setting the cellrenderer "markup" property and then tried adding "\n" and then with the tag. In case of "\n" it prints the "\n" as it is in the treeview cell. And gives following error if I try to add tag.
GtkWarning: Failed to set text from markup due to error parsing markup: Unknown tag 'br' on line 1 char 18
gtk.main()
So how can I add multilines to a gtk treeview cell?
Thanks in advance
With regards to automatically flowing text, CellRendererText should just do the right thing if you set wrap-width property to a value that isn't -1.
If automatic wrapping is not enough and you really need separate paragraphs, I think you may need to implement your own CellRenderer (which is not trivial) or use another container like a ListBox (where custom widgets are easy, but which will mean throwing away your treeview code...).
I have a Tkinter Text() object and I append lines to it using .insert(END, string). When the text fills the available area, I'd expect it to scroll down to show the bottom line of text in the view, but it doesn't scroll (meaning the user has to scroll themselves to see the latest text). I've had a look at the mark_set() method but I can't seem to figure out how to get the cursor to the index of the last item of text.
Any help would be appreciated :)
As usual with Tkinter, there are a number of ways to do this, but one is the easiest: Text.see:
text.insert(END, "spam\n")
text.see(END)
If you want to make sure the start of the new text is visible, not the end of it, or if you only want to do this if the end was already visible beforehand or if the text is active, etc., you may need to look at the other options: scan_mark/scan_dragto, or yview and friends.