使用PyQt5写一段代码,要求定义坐标。右侧使用QLineEdit定义显示框,左侧三个板块,第一个版本PING监测,第二板块TCP监测,第三板块URL监测,每个板块包含5个输入框和两个按钮,点击开始按钮在显示框中显示输入框的内容,代码写完整
时间: 2024-04-30 22:17:30 浏览: 51
抱歉,我可以回答您的问题。
以下是使用 Pyqt5 编写的代码,包含了左侧三个板块,右侧 QLineEdit 显示框和开始按钮的功能。
```python
from PyQt5 import QtWidgets, QtGui, QtCore
class MonitorWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 定义窗口大小和标题
self.setGeometry(300, 300, 800, 600)
self.setWindowTitle('Network Monitor System')
# 定义坐标系统
grid = QtWidgets.QGridLayout()
self.setLayout(grid)
# 左侧三个板块
pingBox = QtWidgets.QGroupBox('PING Monitor')
tcpBox = QtWidgets.QGroupBox('TCP Monitor')
urlBox = QtWidgets.QGroupBox('URL Monitor')
# 定义每个板块内的输入框和按钮
pingBtnStart = QtWidgets.QPushButton('Start')
pingBtnStop = QtWidgets.QPushButton('Stop')
pingEdit1 = QtWidgets.QLineEdit()
pingEdit2 = QtWidgets.QLineEdit()
pingEdit3 = QtWidgets.QLineEdit()
pingEdit4 = QtWidgets.QLineEdit()
pingEdit5 = QtWidgets.QLineEdit()
tcpBtnStart = QtWidgets.QPushButton('Start')
tcpBtnStop = QtWidgets.QPushButton('Stop')
tcpEdit1 = QtWidgets.QLineEdit()
tcpEdit2 = QtWidgets.QLineEdit()
tcpEdit3 = QtWidgets.QLineEdit()
tcpEdit4 = QtWidgets.QLineEdit()
tcpEdit5 = QtWidgets.QLineEdit()
urlBtnStart = QtWidgets.QPushButton('Start')
urlBtnStop= QtWidgets.QPushButton('Stop')
urlEdit1 = QtWidgets.QLineEdit()
urlEdit2 = QtWidgets.QLineEdit()
urlEdit3 = QtWidgets.QLineEdit()
urlEdit4 = QtWidgets.QLineEdit()
urlEdit5 = QtWidgets.QLineEdit()
# 把输入框和按钮添加到每个板块里
pingLayout = QtWidgets.QVBoxLayout()
pingLayout.addWidget(pingEdit1)
pingLayout.addWidget(pingEdit2)
pingLayout.addWidget(pingEdit3)
pingLayout.addWidget(pingEdit4)
pingLayout.addWidget(pingEdit5)
pingLayout.addWidget(pingBtnStart)
pingLayout.addWidget(pingBtnStop)
pingBox.setLayout(pingLayout)
tcpLayout = QtWidgets.QVBoxLayout()
tcpLayout.addWidget(tcpEdit1)
tcpLayout.addWidget(tcpEdit2)
tcpLayout.addWidget(tcpEdit3)
tcpLayout.addWidget(tcpEdit4)
tcpLayout.addWidget(tcpEdit5)
tcpLayout.addWidget(tcpBtnStart)
tcpLayout.addWidget(tcpBtnStop)
tcpBox.setLayout(tcpLayout)
urlLayout = QtWidgets.QVBoxLayout()
urlLayout.addWidget(urlEdit1)
urlLayout.addWidget(urlEdit2)
urlLayout.addWidget(urlEdit3)
urlLayout.addWidget(urlEdit4)
urlLayout.addWidget(urlEdit5)
urlLayout.addWidget(urlBtnStart)
urlLayout.addWidget(urlBtnStop)
urlBox.setLayout(urlLayout)
# 定义 QLineEdit 显示框
self.textbox = QtWidgets.QLineEdit(self)
self.textbox.setReadOnly(True)
# 在坐标系统中添加三个板块和 QLineEdit 显示框
grid.addWidget(pingBox, 0, 0)
grid.addWidget(tcpBox, 1, 0)
grid.addWidget(urlBox, 2, 0)
grid.addWidget(self.textbox, 0, 1, 3, 1)
# 定义开始按钮的事件
pingBtnStart.clicked.connect(lambda: self.addText(pingEdit1.text(), pingEdit2.text(), pingEdit3.text(), pingEdit4.text(), pingEdit5.text()))
tcpBtnStart.clicked.connect(lambda: self.addText(tcpEdit1.text(), tcpEdit2.text(), tcpEdit3.text(), tcpEdit4.text(), tcpEdit5.text()))
urlBtnStart.clicked.connect(lambda: self.addText(urlEdit1.text(), urlEdit2.text(), urlEdit3.text(), urlEdit4.text(), urlEdit5.text()))
# 定义在 QLineEdit 显示框中添加输入框内容的函数
def addText(self, *strings):
text = ' '.join(strings)
if text:
self.textbox.setText(self.textbox.text() + '\n' + text)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = MonitorWindow()
window.show()
app.exec_()
```
阅读全文