0

I want to solve this problem as it will save me a lot of time in both executing codes and retrieving information.The code below show for loop that display a bunch of group boxes and some information inside. What I need is: when I click on one of them , It will send me it's title which it doesn't happen because it always send me the last item in the loop.

from PyQt5.QtWidgets import *


class MyTableWidget(QWidget):

    def __init__(self, parent=None):
        super(QWidget, self).__init__(parent)
        self.layout = QVBoxLayout(self)

        self.tabs = QTabWidget()
        self.tab1 = QWidget()
        self.tab2 = QScrollArea()

        self.tabs.addTab(self.tab1, 'Tab 1')
        self.tabs.addTab(self.tab2, 'Tab 2')

        content_widget = QWidget()
        self.tab2.setWidget(content_widget)
        flay = QVBoxLayout(content_widget)
        self.tab2.setWidgetResizable(True)
        
        nums={1,2,3,4,5,6,7}
        
        #Here the problem

        for n in nums :
            
            self.n = QGroupBox(str(n))
            flay.addWidget(self.n)
            self.n.setCheckable(True)
            #this line particularly 
            self.n.clicked.connect(lambda: self.succ(self.n.title()))


        self.layout.addWidget(self.tabs)
        self.resize(300, 100)

    def succ(self,num):
        print("The coming ",num)



Abdulrahman Dawoud
  • 411
  • 1
  • 4
  • 7
  • Lambda statements are only evaluated at execution: when your lambda is called, `self.n` is the last groupbox of the `for` cycle. Use functool's partial (`self.n.clicked.connect(partial(self.succ, self.n.title()))`) or appropriate keyword arguments (`self.n.clicked.connect(lambda clicked, title=self.n.title(): self.succ(title))`, note the first `clicked` argument, which is required since that signal is always emitted with the button's checked state). See [Connecting slots and signals in PyQt4 in a loop](https://stackoverflow.com/questions/4578861/connecting-slots-and-signals-in-pyqt4-in-a-loop) – musicamante Apr 19 '21 at 02:09
  • Also, please try to improve your research skills, there are dozens of posts like yours about the same identical issue. – musicamante Apr 19 '21 at 02:12

0 Answers0