How can I make true-false on every click in Python?

31 Views Asked by At

I'm trying to improve myself on a project in Python, but I'm stuck somewhere. The problem is; I want the global variable I defined to change to true-false every time I left click with the mouse. This way, I will be able to make a proper selection in my Python window. Right now it selects every row I click on. When I select the second row, I want the first row to be deselected.

init:

    def __Initialize(self):
        self.isSelected = False

Mouse button:

   def OnMouseLeftButtonDown(self):
        self.isSelected = not self.isSelected

OnRender(It works constantly):

    def OnRender(self):
        x, y = self.GetGlobalPosition()
        (widht, height) = self.simulate_page_size
        y_add = self.simulate_page_y
        
        if self.ismyownItem is True:
            grp.SetColor(grp.GenerateColor((153.0/255.0), (204.0/255.0), (255.0/255.0), 0.1))
            grp.RenderBar(x, y + y_add, widht, height)
        elif item_state != 0:
            grp.SetColor(grp.GenerateColor((51.0/255.0), (102.0/255.0), (0.0/255.0), 0.1))
            if item_state != 1:
                grp.SetColor(grp.GenerateColor((255.0/255.0), (128.0/255.0), (0.0/255.0), 0.1))
            grp.RenderBar(x, y + y_add, widht, height)
        elif self.IsIn() or self.message_button.IsIn() or self.money_image.IsIn() or self.icon_image.IsIn() or self.slot_base_image.IsIn():
            grp.SetColor(grp.GenerateColor((86.0/255.0), (75.0/255.0), (71.0/255.0), 0.3))
            grp.RenderBar(x, y + y_add, widht, height)
            
            
        elif self.isSelected: ## I ADD THIS LINE!
            grp.SetColor(grp.GenerateColor((255.0/255.0), (255/255.0), (204/255.0), 0.1))
            grp.RenderBar(x, y + y_add, widht, height)
1

There are 1 best solutions below

0
ori raisfeld On

idk what module you're using, so this might now work, but you can know the current active button instead of every one on it's own, so for example:

selected_button = None
global selected_button
... # other lines of code
   def OnMouseLeftButtonDown(self):
        selected_button = self

now, instead of checking for every button, and activating that way, you can just use selected_button