import sys
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt, QPoint, QTimer, QTime, QDate, QThread
import psutil
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
import subprocess
from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget
import wmi
import urllib.request
import re
class CustomWindow1(QWidget):
def __init__(self):
super().__init__()
# 设置窗口属性
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# 设置窗口大小
self.resize(400, 120)
# 创建两个label控件
self.label1 = QLabel(self)
self.label1.move(20, 20)
self.label1.setFont(QFont('Arial', 12))
self.label2 = QLabel(self)
self.label2.move(20, 60)
self.label2.setFont(QFont('Arial', 12))
# 创建定时器,每秒更新时间和日期
self.timer = QTimer(self)
self.timer.timeout.connect(self.updateTime)
self.timer.start(1000)
# 初始更新时间和日期
self.updateTime()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.NoPen)
# 绘制上半部分背景
painter.setBrush(QColor(43, 55, 64))
painter.drawRoundedRect(0, 0, self.width(), self.height() // 2, 10, 10)
# 绘制下半部分背景
painter.setBrush(QColor(60, 78, 91))
painter.drawRoundedRect(0, self.height() // 2, self.width(), self.height() // 2, 10, 10)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.move(event.globalPos() - self.dragPosition)
def updateTime(self):
# 获取当前时间和日期
current_time = QTime.currentTime()
current_date = QDate.currentDate()
# 更新label控件显示
self.label1.setText(current_time.toString('hh:mm:ss'))
self.label2.setText(
current_date.toString('yyyy-MM-dd') + ' ' + current_date.longDayName(current_date.dayOfWeek()))
class CustomWindow2(QWidget):
def __init__(self):
super().__init__()
# 设置窗口属性
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# 设置窗口大小
self.resize(400, 120)
# 创建两个label控件
self.label1 = QLabel(self)
self.label1.setGeometry(20, 20, 360, 40)
# self.label1.setStyleSheet("background-color: white; border: 1px solid black;")
self.label1.setAlignment(Qt.AlignCenter)
self.label1.setFont(QFont("Arial", 12))
self.label2 = QLabel(self)
self.label2.setGeometry(20, 70, 360, 40)
# self.label2.setStyleSheet("background-color: white; border: 1px solid black;")
self.label2.setAlignment(Qt.AlignCenter)
self.label2.setFont(QFont("Arial", 12))
# 创建定时器,每秒更新CPU和内存使用率
self.timer = QTimer(self)
self.timer.timeout.connect(self.updateUsage)
self.timer.start(1000)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.NoPen)
# 绘制上半部分背景
painter.setBrush(QColor(43, 55, 64))
painter.drawRoundedRect(0, 0, self.width(), self.height() // 2, 10, 10)
# 绘制下半部分背景
painter.setBrush(QColor(60, 78, 91))
painter.drawRoundedRect(0, self.height() // 2, self.width(), self.height() // 2, 10, 10)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drag_position = event.globalPos() - self.frameGeometry().topLeft()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.move(event.globalPos() - self.drag_position)
def updateUsage(self):
# 获取CPU和内存使用率
cpu_percent = psutil.cpu_percent()
mem_percent = psutil.virtual_memory().percent
# 更新label控件显示
self.label1.setText("CPU 使用率: {}%".format(cpu_percent))
self.label2.setText("Memory 使用率: {}%".format(mem_percent))
class CustomWindow3(QWidget):
def __init__(self):
super().__init__()
# 设置窗口属性
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# 设置窗口大小
self.resize(400, 120)
# 创建一个label控件
self.label = QLabel(self)
self.label.setGeometry(20, 20, 360, 80)
self.label.setAlignment(Qt.AlignCenter)
self.label.setFont(QFont("Arial", 6))
# 创建定时器,每秒更新ping的返回信息
self.timer = QTimer(self)
self.timer.timeout.connect(self.updatePing)
self.timer.start(1000)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.NoPen)
# 绘制上半部分背景
painter.setBrush(QColor(43, 55, 64))
painter.drawRoundedRect(0, 0, self.width(), self.height() // 2, 10, 10)
# 绘制下半部分背景
painter.setBrush(QColor(60, 78, 91))
painter.drawRoundedRect(0, self.height() // 2, self.width(), self.height() // 2, 10, 10)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drag_position = event.globalPos() - self.frameGeometry().topLeft()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.move(event.globalPos() - self.drag_position)
def updatePing(self):
# 执行ping命令,获取返回信息
result = subprocess.run(['ping', '-n', '1', 'www.baidu.com'], capture_output=True, text=True)
output = result.stdout
# print(output)
lst = output.split(":") # 分割字符串
# print(lst[0])
# print(lst[1])
# print(lst[2])
# 更新label控件显示
self.label.setText(lst[2])
class CustomWindow4(QWidget):
def __init__(self):
super().__init__()
# 设置窗口属性
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# 设置窗口大小
self.resize(400, 360)
# 创建一个日历控件
self.calendar = QCalendarWidget(self)
self.calendar.setGeometry(20, 20, 360, 320)
self.calendar.setStyleSheet("background-color: rgb(60, 78, 91);"
"QTableView::item:first {background-color: rgb(60, 78, 91);}"
"QTableView::item:selected:active {background-color: rgb(60, 78, 91);}"
"QTableView::item:selected:!active {background-color: rgb(60, 78, 91);}")
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.NoPen)
# 绘制背景
painter.setBrush(QColor(60, 78, 91))
painter.drawRoundedRect(0, 0, self.width(), self.height(), 10, 10)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drag_position = event.globalPos() - self.frameGeometry().topLeft()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.move(event.globalPos() - self.drag_position)
class CustomWindow5(QWidget):
def __init__(self):
super().__init__()
# 设置窗口属性
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# 获取磁盘信息
self.disk_info = self.get_fs_info()
# 初始化鼠标位置
self.dragPosition = None
# 计算窗口高度
self.window_height = 60 + len(self.disk_info) * 20
# 设置窗口大小
self.setFixedWidth(400)
self.setFixedHeight(self.window_height)
# 创建磁盘信息Label
self.label = QLabel(self)
self.label.setText("磁盘信息")
self.label.setFont(QFont("Arial", 8))
self.label.setStyleSheet("color: green;")
self.label.move(20, 20)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.NoPen)
# 绘制上半部分背景
painter.setBrush(QColor(43, 55, 64))
painter.drawRoundedRect(0, 0, self.width(), 60, 10, 10)
# 绘制下半部分背景
painter.setBrush(QColor(60, 78, 91))
painter.drawRoundedRect(0, 60, self.width(), self.height() - 60, 10, 10)
# 绘制磁盘信息文本
painter.setPen(QColor(0, 255, 0)) # 设置颜色为绿色
painter.setFont(QFont("Arial", 8)) # 设置字体大小为8
for i, disk in enumerate(self.disk_info):
disk_name = disk['Caption']
disk_FreeSpace = "{:.2f}".format(disk['FreeSpace'])
text = f"{disk_name} 剩余 {disk_FreeSpace}G"
painter.drawText(QPoint(20, 80 + i * 20), text)
def get_fs_info(self):
"""
获取文件系统信息
包含分区的大小、已用量、可用量、使用率、挂载点信息
"""
tmplist = []
c = wmi.WMI()
for physical_disk in c.Win32_DiskDrive():
for partition in physical_disk.associators("Win32_DiskDriveToDiskPartition"):
for logical_disk in partition.associators("Win32_LogicalDiskToPartition"):
tmpdict = {}
tmpdict["Caption"] = logical_disk.Caption
tmpdict["DiskTotal"] = int(logical_disk.Size) / 1024 / 1024 / 1024
tmpdict["UseSpace"] = (int(logical_disk.Size) - int(logical_disk.FreeSpace)) / 1024 / 1024 / 1024
tmpdict["FreeSpace"] = int(logical_disk.FreeSpace) / 1024 / 1024 / 1024
tmpdict["Percent"] = int(
100.0 * (int(logical_disk.Size) - int(logical_disk.FreeSpace)) / int(logical_disk.Size))
tmplist.append(tmpdict)
return tmplist
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton and self.dragPosition is not None:
self.move(event.globalPos() - self.dragPosition)
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.dragPosition = None
def tianqi(url):
res1 = urllib.request.urlopen(url)
date = res1.read().decode("utf8")
pattern = re.compile(r'value="(.*?)" /')
res2 = re.findall(pattern, date)
return res2[1]
class CustomWindow6(QWidget):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.resize(400, 120)
SHIreweath = tianqi("http://www.weather.com.cn/weather1d/101130301.shtml").split()
layout = QVBoxLayout(self)
self.label1 = QLabel("石河子" + ":" + str(SHIreweath[-2]) + str(SHIreweath[-1]))
self.label1.setAlignment(Qt.AlignCenter)
layout.addWidget(self.label1)
reweath = tianqi("http://www.weather.com.cn/weather1d/101130101.shtml").split()
self.label2 = QLabel("乌鲁木齐" + ":" + str(reweath[-2]) + str(reweath[-1]))
self.label2.setAlignment(Qt.AlignCenter)
layout.addWidget(self.label2)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.NoPen)
painter.setBrush(QColor(43, 55, 64))
painter.drawRoundedRect(0, 0, self.width(), self.height() // 2, 10, 10)
painter.setBrush(QColor(60, 78, 91))
painter.drawRoundedRect(0, self.height() // 2, self.width(), self.height() // 2, 10, 10)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.move(event.globalPos() - self.dragPosition)
if __name__ == "__main__":
app = QApplication(sys.argv)
locX = 1300
# 创建窗口1
window1 = CustomWindow1()
window1.show()
window1.move(locX, 0)
# 创建窗口2
window2 = CustomWindow2()
window2.show()
window2.move(locX, 130)
# 创建窗口3
window3 = CustomWindow3()
window3.show()
window3.move(locX, 260)
# 创建窗口4
window4 = CustomWindow4()
window4.show()
window4.move(locX, 390)
# 创建窗口5
window5 = CustomWindow5()
window5.show()
window5.move(locX, 760)
# 创建窗口6
window6 = CustomWindow6()
window6.show()
window6.move(locX, 910)
sys.exit(app.exec_())
文章来源地址https://www.toymoban.com/news/detail-644307.html
文章来源:https://www.toymoban.com/news/detail-644307.html
到了这里,关于python系统监控程序 时间 CPU内存使用率 硬盘大小 天气 日历的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!