简体   繁体   English

pyqt5 中的 Python 代码输出错误的值

[英]Python code in pyqt5 is outputting the wrong values

here is the GitHub link to download the ui files https://github.com/AbhiTheGreat75/so-pyqt5这里是 GitHub 链接下载 ui 文件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()

ok guys i have found the answer for some reason you cant use lambda in pyqt5 instead you use partial like this好的,伙计们,由于某种原因,我找到了答案,您不能在 pyqt5 中使用 lambda 而不是像这样使用部分

from functools import partial
partial(func_name,arg1,arg2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM