<html dir="ltr"><head></head><body style="text-align:left; direction:ltr;"><div><br></div><div>In the past I have used the following approach which does the trick quite well by using a dict containing instances and colors instead of a list with colors. With self.sender() one can obtain the corresponding color. A functional example can be found below. </div><div><br></div><div>Since the question has been solved allready, I am sending this to find out if my approach can be considerd "good" and if it has any side effect I am currently not aware of....</div><div><br></div><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:9,0pt;"><span style="color:#cc7832;">import </span>sys<br><br><span style="color:#cc7832;">from </span>PyQt5.QtCore <span style="color:#cc7832;">import </span>pyqtSlot<br><span style="color:#cc7832;">from </span>PyQt5.QtGui <span style="color:#cc7832;">import </span>QColor<br><span style="color:#cc7832;">from </span>PyQt5.QtWidgets <span style="color:#cc7832;">import </span>QWidget<span style="color:#cc7832;">, </span>QPushButton<span style="color:#cc7832;">, </span>QColorDialog<span style="color:#cc7832;">, </span>QApplication<span style="color:#cc7832;">, </span>QVBoxLayout<br><br><br><span style="color:#cc7832;">class </span>ButtonColorWidget(QWidget):<br>    <span style="color:#cc7832;">def </span><span style="color:#b200b2;">__init__</span>(<span style="color:#94558d;">self</span><span style="color:#cc7832;">, </span>parent=<span style="color:#cc7832;">None</span>):<br>        <span style="color:#8888c6;">super</span>().<span style="color:#b200b2;">__init__</span>(<span style="color:#aa4926;">parent</span>=parent)<br>        <span style="color:#94558d;">self</span>.layout = QVBoxLayout()<br>        <span style="color:#94558d;">self</span>.setLayout(<span style="color:#94558d;">self</span>.layout)<br>        <span style="color:#808080;"># Create  some sample buttons with color mapping<br></span><span style="color:#808080;">        </span><span style="color:#94558d;">self</span>.button_color_dict = <span style="color:#8888c6;">dict</span>()<br>        <span style="color:#cc7832;">for </span>n <span style="color:#cc7832;">in </span><span style="color:#8888c6;">range</span>(<span style="color:#6897bb;">4</span>):<br>            button = QPushButton(<span style="color:#6a8759;">'Button ' </span>+ <span style="color:#8888c6;">str</span>(n))<br>            <span style="color:#94558d;">self</span>.button_color_dict[button] = QColor(<span style="color:#6a8759;">'black'</span>)  <span style="color:#808080;"># initial_color<br></span><span style="color:#808080;">            </span><span style="color:#94558d;">self</span>.layout.addWidget(button)<br>            button.clicked.connect(<span style="color:#94558d;">self</span>.start_dialog)<br>        <span style="color:#808080;"># ColorDialog<br></span><span style="color:#808080;">        </span><span style="color:#94558d;">self</span>.color_dialog = QColorDialog(<span style="color:#94558d;">self</span>)<br><br>    <span style="color:#bbb529;">@pyqtSlot</span>()<br>    <span style="color:#cc7832;">def </span><span style="color:#ffc66d;">start_dialog</span>(<span style="color:#94558d;">self</span>):<br>        button = <span style="color:#94558d;">self</span>.sender()<br>        <span style="color:#cc7832;">if </span>button <span style="color:#cc7832;">is None</span>:<br>            <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">'Directly calling this method is ignored...'</span>)<br>            <span style="color:#cc7832;">return<br></span><span style="color:#cc7832;">        </span>new_color = <span style="color:#94558d;">self</span>.color_dialog.getColor(<span style="color:#94558d;">self</span>.button_color_dict[button])<br><br>        <span style="color:#cc7832;">if </span>new_color.isValid():<br>            button.setStyleSheet(<span style="color:#6a8759;">'QPushButton{color: ' </span>+ new_color.name()+<span style="color:#6a8759;">'};'</span>)<br>            <span style="color:#94558d;">self</span>.button_color_dict[button] = new_color<br>            <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">'Set new color '</span><span style="color:#cc7832;">, </span>new_color.name())<br>        <span style="color:#cc7832;">else</span>:<br>            <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">'Aborted...'</span>)<br>        <span style="color:#cc7832;">return<br></span><span style="color:#cc7832;"><br></span><span style="color:#cc7832;"><br></span>application = QApplication(sys.argv)<br>widget = ButtonColorWidget()<br>widget.show()<br></pre><div><span style="color: rgb(169, 183, 198); font-family: "DejaVu Sans Mono"; background-color: rgb(43, 43, 43);">sys.exit(application.exec_())</span> </div><div><br></div><div><br></div><div><br></div><div><br></div><div>Am Freitag, den 17.05.2019, 06:34 -0700 schrieb Luna Tuna:</div><blockquote type="cite" style="margin:0 0 0 .8ex; border-left:2px #729fcf solid;padding-left:1ex"><div dir="ltr">Thanks, I'll check it out. <br><div><br></div><div>I just made a local function "connectButton" and that does the trick.  But I like the default lambda parameter for more conciseness.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, May 17, 2019 at 1:06 AM Florian Bruhin <<a href="mailto:me@the-compiler.org">me@the-compiler.org</a>> wrote:<br></div><blockquote type="cite" style="margin:0 0 0 .8ex; border-left:2px #729fcf solid;padding-left:1ex">Hi,<br>
<br>
another possibility is using functools.partial instead:<br>
<br>
    button.clicked.connect(functools.partial(self.show_color_dialog, color))<br>
<br>
IMHO, this is a nice explanation of the issue and possible solutions:<br>
<a href="https://docs.python-guide.org/writing/gotchas/#late-binding-closures" rel="noreferrer" target="_blank">https://docs.python-guide.org/writing/gotchas/#late-binding-closures</a><br>
<br>
Florian<br>
<br>
<pre>_______________________________________________</pre><pre>PyQt mailing list    <a href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a></pre><pre><a href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a></pre><pre><br></pre></blockquote></div></blockquote></body></html>