박형렬
박형렬

Reputation: 397

about pyqt5 about button.clicked.connect

all.

I want to make only blank when i clicked mk_btn the amount is cnt. So I use self.resultTable.setRowCount(cnt) and then if i cliked cs_btn, I want to use function which name is get_in. the get_in needs cnt. but self.cs_btn.clicked.connect(self.get_in(cnt)) don't take cnt. how can I fix it?

please help me.

class MyWindow(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.kiwoom = Kiwoom()
        self.kiwoom.commConnect()
        self.mk_btn.clicked.connect(self.makelist)
        self.cs_btn.clicked.connect(self.get_in(cnt))


    def makelist(self):
        self.kiwoom.getConditionLoad()
        self.kiwoom.sendCondition("0154", self.kiwoom.condition[0], 0, 1)
        cnt = len(self.kiwoom.cl)
        self.resultTable.setRowCount(cnt)
        return cnt

    def get_in(self, cnt):
        print(cnt)
        for i in range(cnt):
            self.kiwoom.dynamicCall("SetInputValue(QString, QString)", "종목코드", self.kiwoom.cl[i])
            self.kiwoom.dynamicCall("CommRqData(QString, QString, int, QString)", "주식기본정보요청", "opt10001", 0, "0101")
            print(self.kiwoom.name2)
            """
            self.resultTable.setItem(i, 0, QTableWidgetItem(self.kiwoom.cl[i]))
            self.resultTable.setItem(i, 1, QTableWidgetItem(self.kiwoom.name2))
            self.resultTable.setItem(i, 3, QTableWidgetItem(self.kiwoom.per2))
            self.resultTable.setItem(i, 4, QTableWidgetItem(self.kiwoom.pbr2))
            """
            time.sleep(0.5)

Upvotes: 0

Views: 291

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

The variable cnt is local so it only exists in that context, if you want to be able to be accessed by another method you must create a member of the class that stores that information since its existence persists until the object is released. In your case:

class MyWindow(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.cnt = 0
        self.kiwoom = Kiwoom()
        self.kiwoom.commConnect()
        self.mk_btn.clicked.connect(self.makelist)
        self.cs_btn.clicked.connect(self.get_in)


    def makelist(self):
        self.kiwoom.getConditionLoad()
        self.kiwoom.sendCondition("0154", self.kiwoom.condition[0], 0, 1)
        self.cnt = len(self.kiwoom.cl)
        self.resultTable.setRowCount(self.cnt)

    def get_in(sel):
        print(self.cnt)
        for i in range(self.cnt):
            [...]

Upvotes: 1

Related Questions