Detecting changes

42 views
Skip to first unread message

floati...@gmail.com

unread,
Nov 22, 2017, 12:58:40 AM11/22/17
to wxPython-users
I have a wx.notebook used as a library database. The user can search for and display the details of a publication, and if necessary amend an entry.
Is there a quick way to know if the user has changed anything? I would like to catch any attempt to close the app if there is un-saved data.
Thanks.

Steve Barnes

unread,
Nov 22, 2017, 1:00:11 AM11/22/17
to wxpytho...@googlegroups.com
Just set a flag on any change and clear it on a save.

--
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect
those of my employer.

---
This email has been checked for viruses by AVG.
http://www.avg.com

floati...@gmail.com

unread,
Nov 22, 2017, 2:30:30 AM11/22/17
to wxPython-users
I don't know how to do that, can you give me a bit more please?

James Scholes

unread,
Nov 22, 2017, 6:04:30 AM11/22/17
to wxpytho...@googlegroups.com
floati...@gmail.com wrote:
> I don't know how to do that, can you give me a bit more please?

The main idea here is that you have a set of widgets; each one of those
widgets fires an event when its content, position, value etc. changes.
But the event type differs by widget. For instance you would bind a
handler to wx.EVT_TEXT to catch changes to a wx.TextCtrl, wx.EVT_SLIDER
to catch changes to the value of a wx.Slider, and the list goes on.

So, the basic idea is to find the events which correspond to the types
of widgets you're using, and bind handlers to them which set a flag
inside your application saying "hey, the user made a change". Call it
unsavedChanges or something along those lines and make it a boolean.
When they edit a value, set it to True. Catch the wx.EVT_CLOSE event so
that when the user tries to exit, you can check the flag and alert them
appropriately. Once they actually save the data, e.g. by pressing
Ctrl+S or whatever mechanism you give them, set the flag back to False.

Regards,

James Scholes
https://twitter.com/JamesScholes

floati...@gmail.com

unread,
Nov 22, 2017, 7:20:40 AM11/22/17
to wxPython-users
Thanks Steve, I have everything set up and working, handlers etc. I just wondered if there was a: "somebody clicked somewhere" catch-all rather than having to modify the code for every widget. Sometimes python and wxpython have nifty shortcuts that you don't know about until you ask. Looks like this isn't one of those occasions.
Thanks.

Tim Roberts

unread,
Nov 22, 2017, 3:04:54 PM11/22/17
to wxpytho...@googlegroups.com
floati...@gmail.com wrote:
>
> Thanks Steve, I have everything set up and working, handlers etc. I
> just wondered if there was a: "somebody clicked somewhere" catch-all
> rather than having to modify the code for every widget. Sometimes
> python and wxpython have nifty shortcuts that you don't know about
> until you ask. Looks like this isn't one of those occasions.

No, because really, you're the only person that knows which parts of
your state are important.  There's no "general rule" that a dialog could
follow.

The process James described is extremely common.  In many property
pages, for example, there will be a "Close" button that changes to
"Apply" if you touch any of the settings, to tell you that something
changed.  It's done by handling events from each of the data-containing
controls individually.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Rufus Smith

unread,
Nov 22, 2017, 3:09:24 PM11/22/17
to wxpytho...@googlegroups.com
You must have some function that populates the data entry screen from your database data.

And a function that creates a new database record from the data on the screen.

Just save a copy of the database record that you populated the screen  from.

When the user exits, create what would be the new database record, and compare it
to the old database record, to see if anything changed.

If your record is just a list, you can compare the lists directly, I believe.

If your record is a dict, you probably have to do field-by-field compares.  Still not too tough.

The flag method Steve mentions is easy too:

Just set a flag on your data (sometimes known as the "dirty" flag).

When you load the data to the screen, have a line:

self.dirty = False

In every event  that could modify, have a simple line like:

self.dirty = True

When you are ready to exit use:

if (self.dirty):
# update the data
 

Rufus


--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

floati...@gmail.com

unread,
Nov 22, 2017, 4:58:04 PM11/22/17
to wxPython-users
Got it, thanks guys. I was just being lazy!



On Wednesday, November 22, 2017 at 5:58:40 AM UTC, floati...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages