【问题标题】:Python code in pyqt5 is outputting the wrong valuespyqt5 中的 Python 代码输出错误的值
【发布时间】:2020-12-07 22:31:43
【问题描述】:

这里是下载 ui 文件的 GitHub 链接 https://github.com/AbhiTheGreat75/so-pyqt5

这段代码的主要问题是,当我单击带有股票名称的按钮时,它会以错误的股票名称打开它。

如何复制:
当您单击运行并运行代码时,单击侧面的其中一个按钮将有一个打印语句告诉您单击了哪个按钮。 问题是打印语句和你点击的按钮不匹配。

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()

【问题讨论】:

  • 您能创建一个minimal reproducible example吗?
  • 实际上它使用其他文件并从那里获取 gui。没有这些文件它不会运行
  • 您需要将问题隔离为一个轻松可复制+可粘贴且可运行的简短脚本
  • 我已按要求编辑了问题

标签: python pyqt5


【解决方案1】:

好的,伙计们,我找到了答案 出于某种原因,您不能在 pyqt5 中使用 lambda 而是像这样使用部分

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

【讨论】:

猜你喜欢
  • 2016-06-27
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-03
相关资源
最近更新 更多