<i id='Nl6AF'><tr id='Nl6AF'><dt id='Nl6AF'><q id='Nl6AF'><span id='Nl6AF'><b id='Nl6AF'><form id='Nl6AF'><ins id='Nl6AF'></ins><ul id='Nl6AF'></ul><sub id='Nl6AF'></sub></form><legend id='Nl6AF'></legend><bdo id='Nl6AF'><pre id='Nl6AF'><center id='Nl6AF'></center></pre></bdo></b><th id='Nl6AF'></th></span></q></dt></tr></i><div id='Nl6AF'><tfoot id='Nl6AF'></tfoot><dl id='Nl6AF'><fieldset id='Nl6AF'></fieldset></dl></div>
    <bdo id='Nl6AF'></bdo><ul id='Nl6AF'></ul>

      <legend id='Nl6AF'><style id='Nl6AF'><dir id='Nl6AF'><q id='Nl6AF'></q></dir></style></legend>

      <small id='Nl6AF'></small><noframes id='Nl6AF'>

      <tfoot id='Nl6AF'></tfoot>

      1. 通过 connect 传递额外的参数

        时间:2023-08-06
        <i id='5935O'><tr id='5935O'><dt id='5935O'><q id='5935O'><span id='5935O'><b id='5935O'><form id='5935O'><ins id='5935O'></ins><ul id='5935O'></ul><sub id='5935O'></sub></form><legend id='5935O'></legend><bdo id='5935O'><pre id='5935O'><center id='5935O'></center></pre></bdo></b><th id='5935O'></th></span></q></dt></tr></i><div id='5935O'><tfoot id='5935O'></tfoot><dl id='5935O'><fieldset id='5935O'></fieldset></dl></div>
        <tfoot id='5935O'></tfoot>

            <legend id='5935O'><style id='5935O'><dir id='5935O'><q id='5935O'></q></dir></style></legend>

              <small id='5935O'></small><noframes id='5935O'>

                <bdo id='5935O'></bdo><ul id='5935O'></ul>

                    <tbody id='5935O'></tbody>
                  本文介绍了通过 connect 传递额外的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  是否可以通过插槽传递变量,以便我可以打印出某些文本?试图将另一个函数中定义的变量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"会根据选择的文件而变化.

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

                  推荐答案

                  问题可以通过两种方式解决:

                  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, ....):
                      [...]
                  

                  地点:

                  • param1, param2, ... : 是信号发送的参数
                  • arg1, arg2, ...: 是你要花费的额外参数

                  在你的情况下:

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

                  <小时>

                  使用functools.partial:

                  一般:


                  Using functools.partial:

                  In general:

                      obj.signal.connect(partial(fun, args1, arg2, ... ))
                  
                  def fun(arg1, arg2, ..., param1, param2, ...):
                      [...]
                  

                  地点:

                  • param1, param2, ... : 是信号发送的参数
                  • arg1, arg2, ...:是您要发送的额外参数

                  在你的情况下:

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

                  这篇关于通过 connect 传递额外的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Python 的控制台中显示 Emoji 下一篇:参数 1 有意外的类型“NoneType"?

                  相关文章

                  最新文章

                    <i id='oRxzi'><tr id='oRxzi'><dt id='oRxzi'><q id='oRxzi'><span id='oRxzi'><b id='oRxzi'><form id='oRxzi'><ins id='oRxzi'></ins><ul id='oRxzi'></ul><sub id='oRxzi'></sub></form><legend id='oRxzi'></legend><bdo id='oRxzi'><pre id='oRxzi'><center id='oRxzi'></center></pre></bdo></b><th id='oRxzi'></th></span></q></dt></tr></i><div id='oRxzi'><tfoot id='oRxzi'></tfoot><dl id='oRxzi'><fieldset id='oRxzi'></fieldset></dl></div>
                      <bdo id='oRxzi'></bdo><ul id='oRxzi'></ul>

                      <small id='oRxzi'></small><noframes id='oRxzi'>

                    1. <tfoot id='oRxzi'></tfoot>
                    2. <legend id='oRxzi'><style id='oRxzi'><dir id='oRxzi'><q id='oRxzi'></q></dir></style></legend>