如何在Python中对每次点击做出true-false?

问题描述 投票:0回答:1

我正在尝试在 Python 项目中提高自己,但我陷入了困境。问题是;我希望每次用鼠标左键单击时,我定义的全局变量都会更改为 true-false。这样,我就能够在 Python 窗口中做出正确的选择。现在它选择我点击的每一行。当我选择第二行时,我希望取消选择第一行。

初始化:

    def __Initialize(self):
        self.isSelected = False

鼠标按钮:

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

OnRender(持续工作):

    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)
python button click mouse onrender
1个回答
0
投票

idk 你正在使用什么模块,所以这现在可能可以工作,但你可以知道当前的活动按钮而不是它自己的每个按钮,所以例如:

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

现在,您可以只使用 selected_button

,而不是检查每个按钮并以这种方式激活
© www.soinside.com 2019 - 2024. All rights reserved.