python自动化web测试-QTableWidget控件的使用

需求

需要在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)  # 设置背景色

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值