python - 通过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
解决方案
您不能.
与表达式一起使用,请使用getattr
:
getattr(self, name).setStyleSheet("...")
推荐阅读
- mysql - 如何加快 MySQL 百万行的插入速度?
- python - 平均图像 ROI 中的像素
- javascript - 如何从数组中找到数组中的特定元素
- dart - 颤振流不听或检测到变化
- java - Spring的application.properties中应该转义哪些字符?
- jquery - 将输入值乘以跨度
- android - 无法启动接收器 com.google.firebase.iid.FirebaseInstanceIdInternalReceiver android Oreo
- c# - 如何从 c# 代码向存储过程参数添加“N”前缀
- php - 在 Ubuntu 16.04 上使用 PHP 7.2 连接到 SQL Server,使用 Microsoft Drivers for PHP for SQL Server,在浏览器中工作但在命令行中失败
- javascript - 在 Python 中将视频源从 Javascript 传递到 OpenCV