鱼C论坛

 找回密码
 立即注册
查看: 2363|回复: 2

这个还能再优化一下吗?

[复制链接]
发表于 2020-10-26 15:53:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 wnhuiyou 于 2020-10-27 10:50 编辑


这是我自己的写的代码,我自己都感觉得很臃肿,可又不知道还能再怎么优化,还存在一个问题就是 在发送指令时 界面就会卡主 估计是没有开线程的关系,可我又不知道怎么转换成线程,下面是完整的代码,希望大家帮我优化一下,然后在开个线程,这样我也可以借鉴学习下。

  1. import os
  2. import sys

  3. from PyQt5.Qt import QWidget, QApplication

  4. from PyQt5.QtWidgets import QPushButton, QLabel, QLineEdit


  5. class SW(QWidget):

  6.     def __init__(self, ):
  7.         super().__init__()

  8.         # 版权信息
  9.         self.label = QLabel(self)
  10.         self.label.setText("Copyright © XXXX科技公司出品")
  11.         self.label.setStyleSheet("font:12px")
  12.         self.label.move(50, 270)

  13.         self.ipaddr = QLineEdit(self)
  14.         self.label2 = QLabel(self)
  15.         self.label2.setText("外网IP:")
  16.         self.label2.setStyleSheet("font:20px")
  17.         self.label2.move(5, 25)

  18.         self.mask = QLineEdit(self)
  19.         self.label3 = QLabel(self)
  20.         self.label3.setText("子网掩码:")
  21.         self.label3.setStyleSheet("font:20px")
  22.         self.label3.move(10, 65)

  23.         self.gateway = QLineEdit(self)
  24.         self.label4 = QLabel(self)
  25.         self.label4.setText("公安网关:")
  26.         self.label4.setStyleSheet("font:20px")
  27.         self.label4.move(10, 105)

  28.     def text(self):

  29.         # 外网IP
  30.         self.ipaddr.setText('')
  31.         self.ipaddr.resize(155, 30)
  32.         self.ipaddr.move(115, 20)
  33.         self.ipaddr.setStyleSheet("font:20px")

  34.         # 子网掩码
  35.         self.mask.setText('255.255.255.0')
  36.         self.mask.resize(155, 30)
  37.         self.mask.move(115, 60)
  38.         self.mask.setStyleSheet("font:20px")

  39.         # 公安网关 默认 192.168.9.1
  40.         self.gateway.setText('192.168.9.1')
  41.         self.gateway.resize(155, 30)
  42.         self.gateway.move(115, 100)
  43.         self.gateway.setStyleSheet("font:20px")

  44.     # 设置路由表
  45.     def SetRoute(self):

  46.         command = '"Route add %s mask %s %s -p"' % (self.ipaddr.text(), self.mask.text(), self.gateway.text())
  47.         os.system(command)

  48.     # 删除路由表
  49.     def DelRoute(self):

  50.         command = '"Route delete %s "' % self.ipaddr.text()
  51.         os.system(command)

  52.     # 查看路由表
  53.     def LookRoute(self):

  54.         command = '"route print"'
  55.         os.system(command)

  56.     # 测试公安网
  57.     def PingGN(self):

  58.         command = '"ping 192.168.100.8"'
  59.         print(os.system(command))

  60.     # 测试外网
  61.     def PingWW(self):

  62.         command = '"ping 114.114.114.114"'
  63.         print(os.system(command))


  64. if __name__ == '__main__':
  65.     app = QApplication(sys.argv)
  66.     win = SW()
  67.     win.setWindowTitle("双网设置")
  68.     win.setFixedSize(300, 290)  # 设定固定窗口大小

  69.     btn = QPushButton(win)
  70.     btn.setText("设置路由表")
  71.     btn.setStyleSheet("font:15px")
  72.     btn.resize(90, 30)
  73.     btn.move(55, 150)
  74.     btn.clicked.connect(lambda: win.SetRoute())

  75.     btn1 = QPushButton(win)
  76.     btn1.setText("删除路由表")
  77.     btn1.setStyleSheet("font:15px")
  78.     btn1.resize(90, 30)
  79.     btn1.move(155, 150)
  80.     btn1.clicked.connect(lambda: win.DelRoute())

  81.     btn2 = QPushButton(win)
  82.     btn2.setText("测试公网")
  83.     btn2.setStyleSheet("font:15px")
  84.     btn2.resize(90, 30)
  85.     btn2.move(155, 190)
  86.     btn2.clicked.connect(lambda: win.PingGN())

  87.     btn3 = QPushButton(win)
  88.     btn3.setText("测试外网")
  89.     btn3.setStyleSheet("font:15px")
  90.     btn3.resize(90, 30)
  91.     btn3.move(55, 190)
  92.     btn3.clicked.connect(lambda: win.PingWW())

  93.     btn4 = QPushButton(win)
  94.     btn4.setText("查看路由表")
  95.     btn4.setStyleSheet("font:15px")
  96.     btn4.resize(90, 30)
  97.     btn4.move(100, 230)
  98.     btn4.clicked.connect(lambda: win.LookRoute())

  99.     win.text()

  100.     win.show()
  101.     sys.exit(app.exec_())
复制代码


1111.png

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-10-28 08:36:59 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-3 16:27:59 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 06:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表