Yujin Kim
Yujin Kim

Reputation: 139

Get color from variable number of buttons PyQt

I'm trying to get the player's names and the color that they choose for a next step in the project. Getting the names is easy enough but the colors is a bit of a pain.
I don't know how to get the selected color and apply it the button using the QColorDialog. The ultimate goal is to get a structure with player name linked to the color they chose.
Here is what i have:

from PyQt5.QtWidgets import (
    QLineEdit,
    QWidget,
    QApplication,
    QLabel,
    QMainWindow,
    QGridLayout,
    QColorDialog,
    QPushButton,
    QVBoxLayout,
    QHBoxLayout,
)
import sys

class NamesPlayers(QMainWindow):
    """ name screen, ask the names of the players """

    def __init__(self, nb_players):
        super().__init__()

        self.layout_widget = QWidget()

        self.player_names = []
        self.player_colors = []

        main_layout = QVBoxLayout()
        names_layout = QGridLayout()
        button_layout = QHBoxLayout()

        button_list = []

        for i in range(nb_players):
            label = QLabel("Name :")
            player_name = QLineEdit()
            color_button = QPushButton("Color")
            color_button.setStyleSheet("background-color: white")
            names_layout.addWidget(label, i, 0)
            names_layout.addWidget(player_name, i, 1)
            names_layout.addWidget(color_button, i, 2)
            button_list.append(color_button)
            self.player_names.append(player_name)
            self.player_colors.append(color_button.styleSheet())

        self.confirm_button = QPushButton("Confirm")

        button_layout.addWidget(self.confirm_button)

        main_layout.addLayout(names_layout)
        main_layout.addLayout(button_layout)

        for button in button_list:
            button.clicked.connect(self.open_colordialog)

        self.layout_widget.setLayout(main_layout)

        self.setCentralWidget(self.layout_widget)

    def open_colordialog(self):
        color_dialog = QColorDialog()
        color_dialog.exec_()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = NamesPlayers(4)
    window.show()
    app.exec_()

My main issue i guess is the fact that the number of buttons for the color can vary and when I click on those buttons, the address for the QColorDialog is always the same which is not what I want.

Any help is appreciated

Upvotes: 2

Views: 182

Answers (1)

eyllanesc
eyllanesc

Reputation: 243945

One possible solution is to use the sender() method to get the button pressed:

import sys

from PyQt5 import QtCore, QtWidgets


class NamesPlayers(QtWidgets.QMainWindow):
    """ name screen, ask the names of the players """

    def __init__(self, nb_players, parent=None):
        super().__init__(parent)

        names_layout = QtWidgets.QGridLayout()

        for i in range(nb_players):
            label = QtWidgets.QLabel("Name :")
            player_name = QtWidgets.QLineEdit()

            color_button = QtWidgets.QPushButton("Color")
            color_button.setStyleSheet("background-color: white")
            color_button.clicked.connect(self.open_colordialog)

            names_layout.addWidget(label, i, 0)
            names_layout.addWidget(player_name, i, 1)
            names_layout.addWidget(color_button, i, 2)

        self.confirm_button = QtWidgets.QPushButton("Confirm")

        central_widget = QtWidgets.QWidget()
        main_layout = QtWidgets.QVBoxLayout(central_widget)
        button_layout = QtWidgets.QHBoxLayout()
        button_layout.addWidget(self.confirm_button)
        main_layout.addLayout(names_layout)
        main_layout.addLayout(button_layout)
        self.setCentralWidget(central_widget)

    @QtCore.pyqtSlot()
    def open_colordialog(self):
        button = self.sender()
        color_dialog = QtWidgets.QColorDialog()
        if color_dialog.exec_() == QtWidgets.QColorDialog.Accepted:
            button.setStyleSheet(
                "background-color: {}".format(color_dialog.selectedColor().name())
            )
        button.clearFocus()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = NamesPlayers(4)
    window.show()
    sys.exit(app.exec_())

Another possible solution is to send the button pressed (with the methods indicated in this answer):

from functools import partial
# ...
for i in range(nb_players):
    # ...
    color_button = QtWidgets.QPushButton("Color")
    color_button.setStyleSheet("background-color: white")
    color_button.clicked.connect(partial(self.open_colordialog, color_button))
    # or
    # color_button.clicked.connect(lambda *args, btn=color_button: self.open_colordialog(btn))
def open_colordialog(self, button):
    color_dialog = QtWidgets.QColorDialog()
    if color_dialog.exec_() == QtWidgets.QColorDialog.Accepted:
        button.setStyleSheet(
            "background-color: {}".format(color_dialog.selectedColor().name())
        )
    button.clearFocus()

Upvotes: 2

Related Questions