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.
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().
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. - import Tkinter
-
-
def text_changed(*args):
-
print tk_name.get()
-
-
top = Tkinter.Tk()
-
-
frame_1 = Tkinter.Frame( top )
-
frame_1.grid(row=0, column=0)
-
-
Tkinter.Label(frame_1, text = "Test Data" ).grid(row=0, column=1)
-
-
tk_name=Tkinter.StringVar()
-
tk_name.set("abcdef")
-
tk_name.trace("w", text_changed)
-
-
entry_1 = Tkinter.Entry(frame_1, textvariable=tk_name)
-
entry_1.grid(row=1, column=1, sticky="W")
-
-
top.mainloop()
bvdet 2,851
Expert Mod 2GB
Cool. I didn't know about the trace method. :)
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.
effbot has an example - import md5
-
def getsignature(contents):
-
return md5.md5(contents).digest()
-
-
text.insert(END, contents) # original contents
-
signature = getsignature(contents)
-
-
...
-
-
contents = text.get(1.0, END)
-
if signature != getsignature(contents):
-
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>>". - from Tkinter import *
-
-
class TextModified():
-
def __init__(self):
-
-
root = Tk()
-
self.txt = Text(master=root)
-
self.txt.pack()
-
self.txt.focus_set()
-
self.txt.bind('<<Modified>>', self.changed)
-
root.mainloop()
-
-
def changed(self, value=None):
-
flag = self.txt.edit_modified()
-
print flag
-
if flag: # prevent from getting called twice
-
print "changed called"
-
## reset so this will be called on the next change
-
self.txt.edit_modified(False)
-
-
TM=TextModified()
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 -
text = Text(root)
-
text.grid()
-
text.bind('<Key>', compare)
-
-
def compare(args*):
-
# check the text
-
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.
As bvdet suggested, use KeyRelease - from Tkinter import *
-
-
def compare(*args):
-
# check the text
-
print "compare", text.get(1.0, END),
-
-
root = Tk()
-
text = Text(root)
-
text.grid()
-
text.bind('<KeyRelease>', compare)
-
text.focus_set()
-
-
root.mainloop()
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.
|
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
...
|
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...
|
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...
|
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----...
|
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----...
|
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...
|
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...
|
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')...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| |