如何解决pyqt5中的Python代码输出错误的值
这是下载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,**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,**kwargs)
uic.loadUi("sell.ui",'label_3')
self.moneygained = self.findChild(QtWidgets.QLabel,'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()
解决方法
好的,我找到了答案 由于某些原因,您不能在pyqt5中使用lambda 相反,您使用像这样的部分
from functools import partial
partial(func_name,arg1,arg2)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。