Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkinter: check modified

Tags:

python

tkinter

I'm a newbie python/tkinter programmer! I am displaying a text widget for the user to use as a barebones editor.

Is it possible to check if the user modified it in any way, so that I know if it necessary a savefile step?

thanks!

alessandro

like image 502
alessandro Avatar asked Jun 29 '11 15:06

alessandro


People also ask

How do I get an event callback when a tkinter entry widget is modified?

We will create an event callback function by specifying the variable that stores the user input. By using the trace("mode", lambda variable, variable: callback()) method with the variable, we can trace the input on the Label widget in the window.

How to check if you have Tkinter installed?

Running python -m tkinter from the command line should open a window demonstrating a simple Tk interface, letting you know that tkinter is properly installed on your system, and also showing what version of Tcl/Tk is installed, so you can read the Tcl/Tk documentation specific to that version.

What is Tk StringVar ()?

StringVar() is a class from tkinter. It's used so that you can easily monitor changes to tkinter variables if they occur through the example code provided: def callback(*args): print "variable changed!" var = StringVar() var.trace("w", callback) var. set("hello")

What is Tk() in Tkinter?

Tkinter is a Python package which comes with many functions and methods that can be used to create an application. In order to create a tkinter application, we generally create an instance of tkinter frame, i.e., Tk(). It helps to display the root window and manages all the other components of the tkinter application.


1 Answers

The easiest thing to do would be to use the Text.edit_modified() method. A simple usage example:

>>> import Tkinter
>>> root = Tkinter.Tk()
>>> frame = Tkinter.Frame(root)
>>> text = Tkinter.Text(frame)
>>> text.pack()
>>> frame.pack()
>>> text.edit_modified()
0
>>> text.insert('1.0', 'some text')
>>> text.edit_modified()
1
>>> text.edit_modified(False)
''
>>> text.edit_modified()
0
like image 182
senderle Avatar answered Sep 22 '22 08:09

senderle