|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 wnhuiyou 于 2020-10-27 10:50 编辑
这是我自己的写的代码,我自己都感觉得很臃肿,可又不知道还能再怎么优化,还存在一个问题就是 在发送指令时 界面就会卡主 估计是没有开线程的关系,可我又不知道怎么转换成线程,下面是完整的代码,希望大家帮我优化一下,然后在开个线程,这样我也可以借鉴学习下。
- import os
- import sys
- from PyQt5.Qt import QWidget, QApplication
- from PyQt5.QtWidgets import QPushButton, QLabel, QLineEdit
- class SW(QWidget):
- def __init__(self, ):
- super().__init__()
- # 版权信息
- self.label = QLabel(self)
- self.label.setText("Copyright © XXXX科技公司出品")
- self.label.setStyleSheet("font:12px")
- self.label.move(50, 270)
- self.ipaddr = QLineEdit(self)
- self.label2 = QLabel(self)
- self.label2.setText("外网IP:")
- self.label2.setStyleSheet("font:20px")
- self.label2.move(5, 25)
- self.mask = QLineEdit(self)
- self.label3 = QLabel(self)
- self.label3.setText("子网掩码:")
- self.label3.setStyleSheet("font:20px")
- self.label3.move(10, 65)
- self.gateway = QLineEdit(self)
- self.label4 = QLabel(self)
- self.label4.setText("公安网关:")
- self.label4.setStyleSheet("font:20px")
- self.label4.move(10, 105)
- def text(self):
- # 外网IP
- self.ipaddr.setText('')
- self.ipaddr.resize(155, 30)
- self.ipaddr.move(115, 20)
- self.ipaddr.setStyleSheet("font:20px")
- # 子网掩码
- self.mask.setText('255.255.255.0')
- self.mask.resize(155, 30)
- self.mask.move(115, 60)
- self.mask.setStyleSheet("font:20px")
- # 公安网关 默认 192.168.9.1
- self.gateway.setText('192.168.9.1')
- self.gateway.resize(155, 30)
- self.gateway.move(115, 100)
- self.gateway.setStyleSheet("font:20px")
- # 设置路由表
- def SetRoute(self):
- command = '"Route add %s mask %s %s -p"' % (self.ipaddr.text(), self.mask.text(), self.gateway.text())
- os.system(command)
- # 删除路由表
- def DelRoute(self):
- command = '"Route delete %s "' % self.ipaddr.text()
- os.system(command)
- # 查看路由表
- def LookRoute(self):
- command = '"route print"'
- os.system(command)
- # 测试公安网
- def PingGN(self):
- command = '"ping 192.168.100.8"'
- print(os.system(command))
- # 测试外网
- def PingWW(self):
- command = '"ping 114.114.114.114"'
- print(os.system(command))
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- win = SW()
- win.setWindowTitle("双网设置")
- win.setFixedSize(300, 290) # 设定固定窗口大小
- btn = QPushButton(win)
- btn.setText("设置路由表")
- btn.setStyleSheet("font:15px")
- btn.resize(90, 30)
- btn.move(55, 150)
- btn.clicked.connect(lambda: win.SetRoute())
- btn1 = QPushButton(win)
- btn1.setText("删除路由表")
- btn1.setStyleSheet("font:15px")
- btn1.resize(90, 30)
- btn1.move(155, 150)
- btn1.clicked.connect(lambda: win.DelRoute())
- btn2 = QPushButton(win)
- btn2.setText("测试公网")
- btn2.setStyleSheet("font:15px")
- btn2.resize(90, 30)
- btn2.move(155, 190)
- btn2.clicked.connect(lambda: win.PingGN())
- btn3 = QPushButton(win)
- btn3.setText("测试外网")
- btn3.setStyleSheet("font:15px")
- btn3.resize(90, 30)
- btn3.move(55, 190)
- btn3.clicked.connect(lambda: win.PingWW())
- btn4 = QPushButton(win)
- btn4.setText("查看路由表")
- btn4.setStyleSheet("font:15px")
- btn4.resize(90, 30)
- btn4.move(100, 230)
- btn4.clicked.connect(lambda: win.LookRoute())
- win.text()
- win.show()
- sys.exit(app.exec_())
复制代码
|
|