How to share info between events

  • Context: Python 
  • Thread starter Thread starter cpscdave
  • Start date Start date
  • Tags Tags
    Events
Click For Summary

Discussion Overview

The discussion revolves around methods for sharing information between mouse events in a Python application using wxPython. Participants explore various approaches to manage event data, particularly focusing on the EVT_LEFT_DOWN and EVT_LEFT_UP events, and consider different data structures for storing this information.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant questions the "proper" way to share information between events and suggests using a class to store data, though they find it potentially sloppy.
  • Another participant seeks clarification on the original question about comparing positions between mouse events.
  • A participant describes their current method of using a dictionary to store event-related data and expresses uncertainty about its effectiveness.
  • Some participants suggest that creating a dedicated class for event information might be a cleaner approach, while others feel that using a dictionary is acceptable given the class size.
  • One participant shares their experience with linking multiple events in a different context and mentions using a structured approach to store related information.
  • Another participant proposes creating a mouse class to handle events and manage event history more effectively, rather than relying solely on the wx.Frame.
  • Some participants discuss the implications of tracking mouse events within a window versus at a broader level, noting that the design choice depends on the specific UI requirements.
  • A participant mentions developing an EventDispatcher to address some challenges they faced, indicating it improved their implementation.
  • Another participant suggests creating an EventListener class to facilitate event handling in future projects.

Areas of Agreement / Disagreement

Participants express a mix of agreement and differing opinions on the best method for sharing event information. While some support the use of dictionaries, others advocate for more structured approaches. The discussion remains unresolved regarding the optimal design for event handling.

Contextual Notes

Participants highlight various assumptions about the size and complexity of the application, which may influence their recommendations. There is also mention of potential limitations in the current implementation that could affect future scalability and maintainability.

Who May Find This Useful

This discussion may be useful for developers working with wxPython who are interested in event-driven programming and seeking strategies for managing event data effectively.

cpscdave
Messages
402
Reaction score
120
What is the "proper" way in python to share information between events?

I'm mostly just playing around atm.
I'd like to compare the most position between a EVT_LEFT_DOWN and EVT_LEFT_UP events
I can use the class to store the information, but that seems a bit sloppy. Is there a better way??
Thanks!
 
Technology news on Phys.org
cpscdave said:
I'd like to compare the most position between a EVT_LEFT_DOWN and EVT_LEFT_UP events
Can you clarify this? It's not clear to me what you're saying.
 
When I'm expecting a chain of subsequent events, for example. Left mouse button down and then left mouse button (click and then release). I'd like to share information between the events.
In this specific case I pull out the x,y coordinate of where the mouse pointer is when the click happens and would like to compare it to where the release happens.

I'm wondering what the best method for storing that information.
What I'm doing right now is I've created a dictionary to dump that data. But I'm not sure if this is the best method to accomplish this.

Here is what I have so far. WHen you click it will put a rectangle on the screen. If you click that rectangle again it will delete it unless you "drag" it to a different location.
So I'm using the self.eventInfo dictionary to store the relevant information between the 2 event handlers.

Python:
import wx

class circle():
    def __init__(self, posX, posY, radius):
        self.posX = posX
        self.posY = posY
        self.radius = radius
class Shapes(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(350, 300))

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_LEFT_DOWN, self.leftClickDown)
        self.Bind(wx.EVT_LEFT_UP,self.leftClickUp)
      
        self.eventInfo = {}
        self.eventInfo['leftClickDown'] = False
        self.cir = {}
        self.count = 0

        self.count+= 1
      
        self.Centre()
        self.Show(True)

      
    def leftClickUp(self,event):
        if( self.eventInfo['leftClickDown'] == False):
            return
        x, y = event.GetPosition()
        if(x == self.eventInfo['xPos'] and y== self.eventInfo['yPos']):
            del self.cir[self.eventInfo['key']]
        else:
            self.cir[self.eventInfo['key']].posX= x
            self.cir[self.eventInfo['key']].posY = y
        self.Refresh()
      
    def leftClickDown(self, event):
        self.eventInfo ={}
        self.eventInfo['leftClickDown'] = False
        x, y = event.GetPosition()
    
        print "Pos: " + str(x) + ", " + str(y)
        for key in self.cir:
            theCir = self.cir[key]
            print "\t" + str(theCir.posX) + " " + str(theCir.posY)
            if(x >= theCir.posX and x <= theCir.posX + 60 and
               y >= theCir.posY and y <= theCir.posY+ 60):
                self.eventInfo['leftClickDown'] = True
                self.eventInfo['key'] = key
                self.eventInfo['xPos'] = x
                self.eventInfo['yPos'] = y
                return
      
        self.cir[self.count] = circle(x-30, y-30, 60)
        self.flaggedKey = False
        self.count += 1
        self.Refresh()
      
    def OnPaint(self, event):
        dc = wx.PaintDC(self)

        for key in self.cir:
            theCir = self.cir[key]
            dc.DrawRectangle(theCir.posX, theCir.posY, theCir.radius, theCir.radius)app = wx.App()
Shapes(None, -1, 'Shapes')
app.MainLoop()

*EDIT* originally I was using circles instead of rectangles. Hence some of the nameing :)
 
The way you're doing it is the way I'd do it. I suspect a purist might prefer you to create a class that has the wx.Frame as a member and store your eventInfo in that class too "alongside" the Frame rather than "inside". Also, I might define a new class rather than use a dict, but that's just my style.
 
  • Like
Likes   Reactions: cpscdave
Perfect thanks!
 
That's exactly how to do it. Like Ibix said, you might benefit from having a separate structure for it, but it's not necessary considering the size of your class.

I wrote software for VOIP, and we often had to link multiple events together: when I get an END, you have to know things like if the INVITE that started the call had SDP, the capabilities of the phone... I stored all of that information in a structure.
 
  • Like
Likes   Reactions: cpscdave
This is just mucking around for the moment, but parts and pieces will get used in what I'm ultimately working on.
Want to try and avoid having to go back and rewriting big chunks cause I've programmed myself into a corner, and this would be my first real attempt at an event driven program :)
 
Oh, in that case, let me make a recommendation. Don't listen to the mouse events themselves, create a mouse class that does all that. Then "subscribe" to the mouse, and whenever the mouse gets an event, it dispatches a event object to all of it's subscribers. This class can include things like information about previous events, if you want to know the previous state of the mouse, it makes a lot more sense to ask a mouse class than to store it in a window.
 
I'll have to look into that.
I'm still wrapping my head around how to layout the program in general.
The good thing is this will be an internal testing tool only so if there are some less than optimal coding, it won't matter quite as much :)
 
  • #10
I think it can make sense to track mouse events in a window, although it depends on your overall UI design. If you have a lot of different widgets in your interface, most non-custom, and you don't want clicks outside your custom widget to affect the history of clicks in that window then it makes sense to record only the events that happen "inside" that widget.

On the other hand, you might want a mouse click event outside a widget to "reset" the recorded events inside a window. In that case you definitely want to catch mouse events at the earliest level you can and let your various widgets decide if they want to handle them, ignore them, or reset their history (or whatever).

It depends what you're trying to do, I think.
 
  • #11
What I need to track for the mouse is pretty straight forward. Right now I'm thinking I can get away with tracking it via events. That may change however :)

I did whip together an EventDispatcher and it fixed 2 hurdles I was having actually :)
 
  • #12
cpscdave said:
I did whip together an EventDispatcher and it fixed 2 hurdles I was having actually :)
Nice, I would make an EventListener class as well, and inherit from it for anything you want to have events. These are usually two of the first classes I write when starting a new project in any language.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 9 ·
Replies
9
Views
818
Replies
58
Views
7K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K