【问题标题】:Python passing values from a method to another methodPython将值从一个方法传递到另一个方法
【发布时间】:2019-03-02 13:10:28
【问题描述】:

我有一个让我发疯的问题。

我有 2 个模块 Main.py 和 Writeagile.py

在 main.py 上,我有一个 Ui_MainWindow 类,它拥有我所有的用户界面。

在 Ui_MainWindow 我有一个方法“on_click_fetchUser”:

def on_click_fetchUser(self):
    _translate = QtCore.QCoreApplication.translate
    api = AgileApi()
    user_email = self.search_agile.text()

    if "@" and "." not in user_email:
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Warning)
        msg.setText("wrong Email")
        msg.exec_()
    else:
        api.fetchUser(email=user_email)

api.fetchUser(email=user_email)进入 Writeagile.py 和 fetchUser 方法:

def fetchUser(self,email):

    msg = QMessageBox()
    msg.setIcon(QMessageBox.Warning)
    agile_user = self.agileCRM("contacts/search/email/{}".format(email), "GET", None, "application/json")  
    ui = Ui_MainWindow()
    MainWindow = QtWidgets.QMainWindow()
    ui.setupUi(MainWindow)
    try:
        fetch_user = json.loads(str(agile_user))
        if email in fetch_user['properties'][4]['value']:
            first = (fetch_user['properties'][0]['value'])
            last = (fetch_user['properties'][1]['value'])
            email = (fetch_user['properties'][4]['value'])

            return ui.agile_ui(first=first,last=last,email=email)

    except ValueError:
        return {msg.setText("User not found in Agile") ,  msg.exec_()}

这工作正常,我想要的 Json 信息被提取并返回到返回 Ui_MainWindow 中的新方法,agile_ui:

def agile_ui(self,first,last,email):
    _translate = QtCore.QCoreApplication.translate
    print(first,last,email)

    self.first_name.setText(_translate("MainWindow", "{}".format(first)))
    self.last_name.setText(_translate("MainWindow", "{}".format(last)))
    self.email.setText(_translate("MainWindow", "{}".format(email)))

到目前为止,一切都如我所愿,而 Print 准确地为我提供了我想要的信息。

但是现在我的问题开始了! 当方法agile_ui() 从 writeagile.fetchUser() “启动” 将为我改变文本的属性:

self.first_name.setText(_translate("MainWindow", "{}".format(first)))
self.last_name.setText(_translate("MainWindow", "{}".format(last)))
self.email.setText(_translate("MainWindow", "{}".format(email)))

什么都别做!!没有错误或什么都没有!什么都没有发生。

如果我删除从 writeagile.fetchUser 传递的所有内容, 并在 Ui_MainWindow 中启动agile_ui,属性起作用,setText 起作用。

希望任何人都可以帮助我。 最好的问候弗雷德里克

【问题讨论】:

  • 那些不是“脚本”——它们是“模块”。它们是有区别的。您不能导入脚本。更重要的是,这些不是“类方法”——它们只是“方法”。这里的差异更大。类方法没有self。这似乎无关紧要,但如果您希望其他人理解您在说什么,您应该了解事物的名称。
  • 别忘了改标题

标签: python python-3.x class oop self


【解决方案1】:

想通了。

需要将返回值改为只返回值, 然后在 on_click_fetchUser 方法上使用返回值:

fetch_user = json.loads(str(agile_user))
if email in fetch_user['properties'][4]['value']:
    first = (fetch_user['properties'][0]['value'])
    last = (fetch_user['properties'][1]['value'])
    email = (fetch_user['properties'][4]['value'])

    return first,last,email


def on_click_fetchUser(self):
    _translate = QtCore.QCoreApplication.translate
    api = AgileApi()
    user_email = self.search_agile.text()

    if "@" and "." not in user_email:
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Warning)
        msg.setText("Ugyldig Email")
        msg.exec_()
    else:
        result = (api.fetchUser(email=user_email))
        self.first_name.setText(_translate("MainWindow", "{}".format(result[0])))
        self.last_name.setText(_translate("MainWindow", "{}".format(result[1])))
        self.email.setText(_translate("MainWindow", "{}".format(result[2])))

【讨论】:

  • 如果您自己找到问题的最佳答案,可以“接受”您自己的答案。这实际上是鼓励的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多