需求
需要在pyqt界面上显示一个表格,记录的是以ip作为设备标识,以颜色区分设备状态
图示
代码接口
from PyQt5.QtWidgets import QApplication, QMainWindow,QTableWidgetItem
from PyQt5.QtWidgets import QFileDialog,QTableWidgetItem,QAbstractItemView,QHeaderView
online_color = QBrush(QColor(95, 215, 169))
offline_color = QBrush(QColor(220, 220, 220))
'''
函数名:table_init
功能:
初始化界面表格
设置行列数
设置表头隐藏
表格内容填充
参数:无
返回值:无
修改时间:
2020年5月6日10:26:32
'''
def table_init(self):
rows = 5
cols = 12
text_font = QFont("song",8,QFont.Bold)
self.tableWidget.setObjectName("table IP")
self.tableWidget.setColumnCount(cols)
self.tableWidget.setRowCount(rows)
self.tableWidget.setCornerButtonEnabled(False)
self.tableWidget.horizontalHeader().setHighlightSections(False)
self.tableWidget.verticalHeader().setHighlightSections(False)
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)#设置表头自适应伸缩模式
self.tableWidget.verticalHeader().setSectionResizeMode(QHeaderView.Stretch)
ip = ["192.168.3."] * 60
for i in range(60):
ip[i] = ip[i] + str(i+151)
for i in range(rows):
for j in range(cols):
self.tableWidget.setItem(i,j,QTableWidgetItem(ip[i*12+j]))
for val in ip:
items = self.tableWidget.findItems(val, Qt.MatchExactly)
if len(items) > 0:
for i in items:
i.setBackground(offline_color) # 设置背景色
# i.setForeground(QBrush(QColor(255, 0, 0))) # 设置字体颜色
self.tableWidget.setFont(text_font)
'''
函数名:ip_filter
功能:在线设备过滤
参数:无
返回值:无
修改时间:
2020年5月6日14:13:41
2020年5月6日15:27:17
使用全局字典保存测试过滤的结果
ip_online_dictionary = {}
'''
def ip_filter(self):
threads = []
IP = ["192.168.3."] * 60
for i in range(len(IP)):
IP[i] = str.format("%s%d"%(IP[i],i+150))
s = "创建线程:" + IP[i]
printf(s)
thd1 = threading.Thread(target=self.login_test, args=(IP[i],2)) # 创建一个线程
threads.append(thd1)
for th in threads:
th.start() # start()---启动线程活动 GDJC6880
time.sleep(5)
for th in threads:
th.join() # 等待线程结束
passArr,online_device_num = self.set_table_online_ip(IP)
self.online_device_Edit.setText(str.format("%d"%online_device_num))
self.show_table_online_ip(passArr)
'''
函数名:set_table_online_ip
功能:设备在线ip
参数:无
返回值:无
修改时间:
2020年5月6日14:13:41
'''
def set_table_online_ip(self,ipArry):
lens = len(ipArry)
ipArryOnline = [""]*lens
arryIndex = 0
for i in range(lens):
ip_online_dictionary.get(ipArry[i])
if ip_online_dictionary.get(ipArry[i]) == "pass":
ipArryOnline[arryIndex] = ipArry[i]
print("ipArryOnline[%d]:%s"%(i,ipArry[i]))
arryIndex += 1
return ipArryOnline,arryIndex
'''
函数名:show_table_online_ip
功能:获取表格中在线的ip
参数:无
返回值:无
修改时间:
2020年5月6日14:13:41
'''
def show_table_online_ip(self,ipArry):
for ip in ipArry:
items = self.tableWidget.findItems(ip, Qt.MatchExactly)
if len(items) > 0:
for i in items:
i.setBackground(online_color) # 设置背景色