本文介紹了通過(guò) connect 傳遞額外的參數(shù)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
是否可以通過(guò)插槽傳遞變量,以便我可以打印出某些文本?試圖將另一個(gè)函數(shù)中定義的變量DiffP"傳遞給插槽.
Is it possible to pass variables through slots so I can print out certain text? Trying to pass variable 'DiffP' which is defined in another function to slot.
DiffP"會(huì)根據(jù)選擇的文件而變化.
'DiffP' changes based on which file is selected.
def addLineEdit(self):
try:
self.clearLayout()
self.FileButton ={}
self.Input = {}
self.TotalInput = []
for i in range(int(self.numberLine.text())):
self.FileButton[i] = QtWidgets.QPushButton(self.centralwidget)
self.FileButton[i].setText('Case {}'.format(i+1))
self.FileButton[i].setFlat(True)
self.FileButton[i].setMaximumSize(QtCore.QSize(50, 50))
self.hboxlayout[0].addWidget(self.FileButton[i])
self.FileButton[i].clicked.connect(lambda i=i: self.openfile(i))
self.buttonGroup.addButton(self.FileButton[i],i)
self.buttonGroup.buttonClicked['int'].connect(self.input)
def searchfile(self,dir):
with open(dir) as f:
content = f.readlines()
MainList = content[44].split()
RPM = round(float(MainList[0]), 2)
Ps = round(float(MainList[1]), 2)
Ts = round(float(MainList[2]), 2)
Pd = round(float(MainList[3]), 2)
Ratio = round(Pd / Ps, 2)
DiffP = round(Pd - Ps, 2)
@pyqtSlot(int)
def input(self,button_or_id,DiffP):
if isinstance(button_or_id, int):
if button_or_id == 0:
self.TotalInput[0].setText(str(DiffP))
elif button_or_id == 1:
self.TotalInput[54].setText('1')
def openfile(self,i):
filename = QtWidgets.QFileDialog.getOpenFileName(self, 'Choose file')
dir = filename[0]
directory = os.path.split(dir)[0]
return self.searchfile(dir)
推薦答案
問(wèn)題可以通過(guò)兩種方式解決:
The problem can be solved in 2 ways:
一般:
obj.signal.connect(lambda param1, param2, ..., arg1=val1, arg2= value2, ... : fun(param1, param2,... , arg1, arg2, ....))
def fun(param1, param2,... , arg1, arg2, ....):
[...]
地點(diǎn):
- param1, param2, ... : 是信號(hào)發(fā)送的參數(shù)
- arg1, arg2, ...: 是你要花費(fèi)的額外參數(shù)
在你的情況下:
self.buttonGroup.buttonClicked['int'].connect(lambda i: self.input(i, "text"))
@pyqtSlot(int)
def input(self, button_or_id, DiffP):
if isinstance(button_or_id, int):
if button_or_id == 0:
self.TotalInput[0].setText(DiffP)
elif button_or_id == 1:
self.TotalInput[54].setText('1')
<小時(shí)>
使用functools.partial
:
一般:
Using functools.partial
:
In general:
obj.signal.connect(partial(fun, args1, arg2, ... ))
def fun(arg1, arg2, ..., param1, param2, ...):
[...]
地點(diǎn):
- param1, param2, ... : 是信號(hào)發(fā)送的參數(shù)
- arg1, arg2, ...:是您要發(fā)送的額外參數(shù)
在你的情況下:
from functools import partial
[...]
self.buttonGroup.buttonClicked['int'].connect(partial(self.input, "text"))
@pyqtSlot(int)
def input(self, DiffP, button_or_id):
if isinstance(button_or_id, int):
if button_or_id == 0:
self.TotalInput[0].setText(DiffP)
elif button_or_id == 1:
self.TotalInput[54].setText('1')
這篇關(guān)于通過(guò) connect 傳遞額外的參數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!