首页 > 解决方案 > 通过python 3.7.6中的函数传递字符串变量

问题描述

帮助!我试图通过函数调用 ping("name") 传递字符串,这样我就可以将字符串 "compute1_led" 传递给 "computer2_led".. 以此类推,因为我正在从文本文件中读取。我尝试了不同的选择,但没有运气。我将不胜感激任何帮助。

def ping(self,IP,name):

    #Do ping with "IP"

    if "Lost = 0" in resp_ascii:
    self.('%s'%name).setStyleSheet("background-color: rgb(85, 255, 0);\n" "") # LED green <==problem
    
    else:
    self.('%s'%name).setStyleSheet("background-color: rgb(255, 0, 0);\n" "")  #LED Red  <==problem

标签: pythonstringfunction

解决方案


您不能.与表达式一起使用,请使用getattr

getattr(self, name).setStyleSheet("...")

推荐阅读