I'm new to this forum and to python in general, so be easy on me please. :icon_smile:
I'm making a class to handle all events including mouse events. (I'm using pygame by the way)

Heres how it looks so far:

class Event:
    #controlls all actions relating to events including
    #mouse events
    
    def __init__(self):
        #mouse vars
        self.mouse_p = [0,0,0] #previous pressing state of buttons
        self.mouse_a = [0,0,0] #whether the buttons are pressed
        self.mouse_t = [0,0,0] #time the buttons are pressed
        self.mouse_c = [0,0,0] #whether buttons are clicked (in that step)
        self.mouse_r = [0,0,0] #whether buttons are released

    def step(self):
        pygame.event.get()
        self.mouse_a = list(pygame.mouse.get_pressed())
        print self.mouse_a
        for i in range(3):
            if (self.mouse_p[i] == 0)and(self.mouse_a[i] == 1):#mouse active
                self.mouse_c[i]=1
            else:
                self.mouse_c[i]=0
                
            if (self.mouse_p[i] == 1)and(self.mouse_a[i] == 0):#mouse released
                self.mouse_c = 1
            else:
                self.mouse_c = 0
                
            if(self.mouse_a[i]==1): #mouse time
                self.mouse_t[i] +=1
            else:
                self.mouse_t[i]=0
        self.mouse_p = self.mouse_a

The problem is, when I test it, "self.mouse_c=0, TypeError: 'int' object does not support item assignment" comes up. I really don't know why this is. I assume that the lists are immutable inside the "step" method, but why would this be: why wouldn't it have full access to its own variables?

The only way I can think of solving this this to replace each list in one go rather than trying to set the values of each index of the list... but surely there is some other way.

If you could help, it would be greatly appreciated!

Recommended Answers

All 2 Replies

I assume that the lists are immutable inside the "step" method, but why would this be

You are right to doubt that - they aren't.

Look at your lines 24 and 26, there you will find

self.mouse_c = 1
else:
    self.mouse_c = 0

whereas it should have been

self.mouse_c[i] = 1
else:
    self.mouse_c[i] = 0

You assign integers in one loop cycle, and get the error in the next, when you try to treat them as lists.

Reminder (just in case): If you think your issue is resolved, don't forget to mark the thread as 'solved'.

OH, MY, GOD!!!

I can't believe I did that! Thats so stupid of me! Do you know how long I was looking through that? - I guess that explains why I'm so bad at word searches.

Well, thank you so much Pythopian!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.