473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Tkinter text widget - check if the text is edited

Using Tkinter in Python 2.7.

What I need is to be able to check regularly if the text in the widget has been changed. Ideally, I would have been able to call a function each time a change to the text was made or the widget was used, but this seems impossible.

More specifically, I want to be able to have a label change between "original", "altered", "empty" and "new". Where e.g. "original" refers to a text that was fetched from the Internet, "altered" refers to an "original" that has been edited, and "new" is activated if text is typed in manually after the widget has been "empty".

Creating a new thread manually to do the comparisons has crossed my mind, but I am not very familiar with threading and was hoping that there was a more elegant way to accomplish this.
Mar 15 '12 #1
8 22381
bvdet
2,851 Expert Mod 2GB
Off the top of my head - Bind the widget to a 'KeyRelease' event which would set a flag that the user modified the text in some way. Also, you could save the original text as an attribute and compare the widget text at selected intervals in a callback using method after().
Mar 15 '12 #2
dwblas
626 Expert 512MB
You did not say which widget you are using, but for an Entry widget, or any widget with a StringVar, you can set the trace option.
Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2.  
  3. def text_changed(*args):
  4.     print tk_name.get()
  5.  
  6. top = Tkinter.Tk()
  7.  
  8. frame_1 = Tkinter.Frame( top )
  9. frame_1.grid(row=0, column=0)
  10.  
  11. Tkinter.Label(frame_1, text = "Test Data" ).grid(row=0, column=1)
  12.  
  13. tk_name=Tkinter.StringVar()
  14. tk_name.set("abcdef")
  15. tk_name.trace("w", text_changed)
  16.  
  17. entry_1 = Tkinter.Entry(frame_1, textvariable=tk_name)
  18. entry_1.grid(row=1, column=1, sticky="W")
  19.  
  20. top.mainloop() 
Mar 15 '12 #3
bvdet
2,851 Expert Mod 2GB
Cool. I didn't know about the trace method. :)
Mar 15 '12 #4
Per the title, I am using the Text widget (the text will normally contain several lines). The Text widget does not appear to have a 'textvariable' option.

Going to try out bvdet's suggestions now.
Mar 15 '12 #5
dwblas
626 Expert 512MB
effbot has an example
Expand|Select|Wrap|Line Numbers
  1. import md5
  2.     def getsignature(contents):
  3.         return md5.md5(contents).digest()
  4.  
  5.     text.insert(END, contents) # original contents
  6.     signature = getsignature(contents)
  7.  
  8.     ...
  9.  
  10.     contents = text.get(1.0, END)
  11.     if signature != getsignature(contents):
  12.         print "contents have changed!" 
Text widgets can also be bound to "<<Modified>>". I only have one test program so obviously have not done much with this binding, and there is likely a cleaner way to do this, but you can probably find more what you want with a Google for "<<Modified>>".
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. class TextModified():
  4.     def __init__(self):
  5.  
  6.         root = Tk()
  7.         self.txt = Text(master=root)
  8.         self.txt.pack()
  9.         self.txt.focus_set()
  10.         self.txt.bind('<<Modified>>', self.changed)
  11.         root.mainloop()
  12.  
  13.     def changed(self, value=None):
  14.         flag = self.txt.edit_modified()
  15.         print flag
  16.         if flag:     # prevent from getting called twice
  17.             print "changed called"
  18.         ## reset so this will be called on the next change
  19.         self.txt.edit_modified(False)
  20.  
  21. TM=TextModified() 
Mar 15 '12 #6
By the gods, I have been reading the articles on effbot since I started creating my program, but my CTRL+F-ing did not discover that bit about md5.

I wanted to report back that bvdet's suggestion, something a la

Expand|Select|Wrap|Line Numbers
  1. text = Text(root)
  2. text.grid()
  3. text.bind('<Key>', compare)
  4.  
  5. def compare(args*):
  6.        # check the text
  7.  
seems to do the trick, apart from the fact that the text that 'compare' will get its hands on is the text prior to the key being pressed (apparently).

Should be a simple workaround for that, though md5/modified will perhaps spare me that work.
Mar 15 '12 #7
dwblas
626 Expert 512MB
As bvdet suggested, use KeyRelease
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. def compare(*args):
  4.     # check the text
  5.     print "compare", text.get(1.0, END),
  6.  
  7. root = Tk()
  8. text = Text(root)
  9. text.grid()
  10. text.bind('<KeyRelease>', compare)
  11. text.focus_set()
  12.  
  13. root.mainloop() 
Mar 16 '12 #8
Right, that works. :-D

It wouldn't "support" copy and paste through right click, but I am not sure if I am going to implement that anyway (and I guess <<modified>> would support it).

So I'll consider the case solved for the time being. Thanks for the input.
Mar 16 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Jane Austine | last post by:
As you add more items, say text lines, in Text widget, it gets too slow and almost impractical to use on. Take idle for example. If the text gets bigger(e.g. print...
1
by: snx7s | last post by:
Hi, using Tkinter, on a Text widget i would like to place the cursor to a given position like the icursor() method of the Entry widget. Is it possible do that, if it is how please ? Thank You.
3
by: bigbinc | last post by:
I have used the 'entry' tk widget to get text values, I am now using 'Text' but I cant seem to use 'get' method. The TK docs say use get(index1, index2), I tried numbers and get an error ...
2
by: Tonino | last post by:
Hi, I have a small Tkinter app that gets data from a socket connection to a "server". The app has a Text() widget to display the info that it gets from the socket connection. I have the...
2
by: wanwan | last post by:
I need my GUI to open and display a text file. I'm thinking of using a text widget but it looks so complicated in the tkinter manual. question I have is: is there an example anyone can find on...
0
by: sullivanz.pku | last post by:
Hi all I am using the standard python GUI Tkinter as my program's main interface. Although I know wxPython has some widget to support rich text widget, but I do not have time to shift to wx----...
4
by: sullivanz.pku | last post by:
Hi all I am using the standard python GUI Tkinter as my program's main interface. Although I know wxPython has some widget to support rich text widget, but I do not have time to shift to wx----...
5
by: goldtech | last post by:
I thought the "DISABLED" made it so I could not edit it. But it also makes it so I can not scroll down. If you make the window smaller than the content then try to put a cursor in there to use...
3
by: mariox19 | last post by:
Are Tkinter widgets running on their own thread? If I try to make a simple application that will print the letters A to Z to a Tkinter Text widget, and I space the printing of each letter by 1...
0
by: John McMoangle | last post by:
Hey guys. I am having trouble understanding the get() method from The arguments to the get method of the Text widget are strings in the form of "line.position". So, text.get('1.5', '2.7')...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.