简体   繁体   English

通过 python 3.7.6 中的 function 传递字符串变量

[英]Passing string variable through function in python 3.7.6

Help.帮助。 I am trying to pass string through function call ping( "name") so I can pass string "compute1_led" to "computer2_led".. so on as I am reading from a text file.我试图通过 function call ping("name") 传递字符串,这样我就可以将字符串 "compute1_led" 传递给 "computer2_led".. 以此类推,因为我正在从文本文件中读取。 I try different option with no luck.我尝试了不同的选择,但没有运气。 Any help I would appreciated.我将不胜感激任何帮助。

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

You can't use .你不能使用. with an expression, use getattr :使用表达式,使用getattr

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM