Search code examples
pythonpyqtpyqt5qpushbutton

How to cancel the action of a selected button (QPushButton) when a new button is clicked in pyqt5?


`class MainWindow(QWidget): def init(self): super().init()

    self.setWindowTitle("Window")
    self.setGeometry(600, 100, 600, 400)

    self.Function()

def Function(self):
    xpos = 10

    for i in range(5):
    
        button = QPushButton("".format(i+1), self)
        button.setGeometry(xpos, 150, 50, 50 )
        xpos = xpos + 50
        button.setStyleSheet("border : 1px solid black;background-color : green; border-radius : 25px")
        button.clicked.connect(lambda ch, i=i+1: self.function(i))     
        button.clicked.connect(lambda ch, i=button : self.color(i))     

     
def function(self, i):
    print(i)

def color(self, i):
    i.setStyleSheet("border : 1px solid black;background-color : black; border-radius : 25px")`i am new to this sort of programming, and 

i am trying to create a row of buttons (number of buttons are given as input) that are aligned side by side. A button (QPushButton) that is once clicked should take on a color, and when an other button is clicked, the previous action should be canceled and the new selected button must be colored.

I was able to use the "btn.clicked.connect(...)" method to make the selected button get colored. But when the other buttons are clicked the previous ones keep the color without going into default. How do i have to do it correctly?


Solution

  • The way you've declared button it's hard to refer to it later on. I can think of two ways around this:

    1. put your buttons in a list
    2. put your buttons in a QButtonGroup

    put your buttons in a list

    class MainWindow(QWidget): 
    
        def __init__(self): 
            super().__init__()
            self.setWindowTitle("Window")
            self.setGeometry(600, 100, 600, 400)
            self.Function()
    
        def Function(self):
            xpos = 10
    
        self.button_list = []
    
        for i in range(5):
            button = QPushButton("{}".format(i+1), self)
            button.setGeometry(xpos, 150, 50, 50 )
            xpos = xpos + 50
            button.setStyleSheet("border : 1px solid black;background-color : green; border-radius : 25px")
            self.button_list.append(button)
            button.clicked.connect(lambda ch, i=i+1: self.function(i))     
            button.clicked.connect(lambda ch, i=button : self.color(i))     
         
        def function(self, i):
            print(i)
    
        def color(self, i):
            bnum = int(i.text())
            for idx, button in enumerate(self.button_list):
                if idx+1 == bnum:
                    button.setStyleSheet("border : 1px solid black;background-color : black; border-radius : 25px")
                else:
                    button.setStyleSheet("border : 1px solid black;background-color : green; border-radius : 25px")
    

    put your buttons in a QButtonGroup

    class MainWindow(QWidget): 
    
        def __init__(self): 
            super().__init__()
            self.setWindowTitle("Window")
            self.setGeometry(600, 100, 600, 400)
            self.Function()
    
        def Function(self):
            xpos = 10
    
        self.button_group = QButtonGroup()
    
        for i in range(5):
            button = QPushButton("{}".format(i+1), self)
            button.setGeometry(xpos, 150, 50, 50 )
            xpos = xpos + 50
            button.setStyleSheet("border : 1px solid black;background-color : green; border-radius : 25px")
            self.button_group.addButton(button, id=i+1)
            self.button_group.idClicked.connect(self.color)
            self.button_group.idClicked.connect(self.function)
         
        # arguments have changed bid is the button id assigned in addButton
        def function(self, bid):
            print(self.button_group.button(bid))
    
        def color(self, bid):
            bcount = len(self.button_group.buttons())
            for i in range(bcount)
                if i+1 == bid:
                    button.setStyleSheet("border : 1px solid black;background-color : black; border-radius : 25px")
                else:
                    button.setStyleSheet("border : 1px solid black;background-color : green; border-radius : 25px")
    

    a little more efficient...

    Both cases as I've coded them are a little kludgy since they go though ALL of the buttons every time you click ONE button. If you were to save the last button pressed, then you would only have to modify the last button and the current button:

    btn = self.button_group.button(self.last_bid)
    btn.setStyleSheet("border : 1px solid black;background-color : green; border-radius : 25px")
    
    btn = self.button_group.button(bid)
    btn.setStyleSheet("border : 1px solid black;background-color : black; border-radius : 25px")
    
    self.last_bid = bid