简体   繁体   中英

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?

One way you can do it is by using the self.sender() method. This will tell you which button was clicked. Then you can loop through the other buttons and change their style sheet to some default color.

class MainWindow(QtWidgets.QMainWindow, ui_MainWindow):
    def __init__(self):
   
        QtWidgets.QMainWindow.__init__(self)
        ui_MainWindow.__init__(self)
        self.setupUi(self)

        self.pushButton_1.clicked.connect(self.color_buttons)
        self.pushButton_2.clicked.connect(self.color_buttons)
        self.pushButton_3.clicked.connect(self.color_buttons)
    
        self.button_list = [self.pushButton_1, self.pushButton_2, self.pushButton_3]

        self.button_default_css  = "background-color: rgb(225, 225, 225);"
        self.button_selected_css = "background-color: rgb(102, 176, 54);"
    

    def color_buttons(self):
        print(f"\nButton Pressed : {self.sender().objectName()}")
        for button in self.button_list:
            if button == self.sender():
                button.setStyleSheet(self.button_selected_css)
            else:
                button.setStyleSheet(self.button_default_css) 

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

btn.clicked.disconnect() 

The above code probably will work for you. For the further questions you are going to ask, do not forget to add your sample code:

https://stackoverflow.com/help/minimal-reproducible-example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM