-3

here is the GitHub link to download the ui files https://github.com/AbhiTheGreat75/so-pyqt5

The main problem with this code is that when I click the button with the stocks name, it opens it with the wrong stocks name.

How to reproduce:
When you click run and run the code, click one of the buttons in the side there will be a print statement telling which button you clicked. The problem is that print statement and the button you click don't match up.

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import uic
from yahoo_fin import stock_info as si #pip install yahoo_fin
class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        uic.loadUi("main.ui", self)
        self.profile_pic = self.findChild(QtWidgets.QLabel, "profile_pic")
        self.name = self.findChild(QtWidgets.QLabel, "name_3")
        self.money = self.findChild(QtWidgets.QLabel, "money_3")
        self.edit_account = self.findChild(QtWidgets.QPushButton, "edit_account")
        self.watchlistlayout = self.findChild(QtWidgets.QFormLayout, "watchlist")
        self.stockslistlayout = self.findChild(QtWidgets.QFormLayout, "stocks_owned")
class buyWindow(QtWidgets.QDialog):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        uic.loadUi("buy.ui", self)
        self.amount = self.findChild(QtWidgets.QSpinBox, 'spinBox')
        self.totalprice = self.findChild(QtWidgets.QLabel, 'label_3')
        self.moneyleft = self.findChild(QtWidgets.QLabel, 'label_4')
        self.cancel = self.findChild(QtWidgets.QPushButton, 'pushButton_2')
        self.buy = self.findChild(QtWidgets.QPushButton, 'pushButton')
        self.error = self.findChild(QtWidgets.QLabel, 'label_5')
        self.cancel.clicked.connect(self.close)
class sellWindow(QtWidgets.QDialog):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        uic.loadUi("sell.ui", self)
        self.amount = self.findChild(QtWidgets.QSpinBox, 'spinBox')
        self.totalprice = self.findChild(QtWidgets.QLabel, 'label_3')
        self.moneygained = self.findChild(QtWidgets.QLabel, 'label_4')
        self.cancel = self.findChild(QtWidgets.QPushButton, 'pushButton_2')
        self.sell = self.findChild(QtWidgets.QPushButton, 'pushButton')
        self.cancel.clicked.connect(self.close)

class Controller():
    def __init__(self):
        self.watchlist = [['aapl', '2'], ['csco', '5']]
        self.stockslist = [['btc-usd', '2', '222'], ['aapl', '1', '459']]
        app = QtWidgets.QApplication([])
        self.main = MainWindow()
        self.arrange_main()
        self.main.show()
        app.exec_()
    def arrange_main(self):
        if len(self.stockslist) == 0:
            l = QtWidgets.QLabel("You dont Have Any stocks")
            l.setAlignment(QtCore.Qt.AlignCenter)
            self.main.stockslistlayout.addRow(l)
        else:
            i = 0
            for pair in self.stockslist:
                i += 1
                ticker,amount,op=pair
                stockslistbutton=QtWidgets.QPushButton(ticker)
                stockslistbutton.clicked.connect(lambda: self.show_sell(ticker))
                l=QtWidgets.QLabel(str(round(si.get_live_price(ticker),2)))
                self.main.stockslistlayout.addRow(stockslistbutton,l)

        if len(self.watchlist) == 0:
            l = QtWidgets.QLabel("You dont Have Any stock\non watchlist")
            l.setAlignment(QtCore.Qt.AlignCenter)
            self.main.watchlistlayout.addRow(l)
        else:
            for pair in self.watchlist:
                i += 1
                ticker,amount=pair
                watchlistbutton=QtWidgets.QPushButton(ticker)
                watchlistbutton.clicked.connect(lambda: self.show_buy(ticker))
                l=QtWidgets.QLabel(str(round(si.get_live_price(ticker),2)))
                self.main.watchlistlayout.addRow(watchlistbutton,l)
        self.main.name.setText("a")
        self.main.money.setText(str("100"))
    def show_buy(self, ticker):
        self.buy = buyWindow()
        print('button pressed ',ticker)
        self.buy.show()
    def show_sell(self, ticker):
        self.sell = sellWindow()
        print("button pressed ", ticker)
        self.sell.show()

Controller()
4
  • 4
    Could you create a minimal reproducible example, please? Commented Aug 18, 2020 at 17:52
  • Actually it uses other files and takes the gui from there.without those files it won’t run Commented Aug 18, 2020 at 19:15
  • you need to isolate to the problem into a short script that is easily copy+pastable and runnable Commented Aug 18, 2020 at 21:25
  • i have edited the qustion as requested Commented Aug 18, 2020 at 22:28

1 Answer 1

-1

ok guys i have found the answer for some reason you cant use lambda in pyqt5 instead you use partial like this

from functools import partial
partial(func_name,arg1,arg2)
Sign up to request clarification or add additional context in comments.

1 Comment

i got the answer from here stackoverflow.com/questions/6784084/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.