import tkinter as tkimport psutilimport GPUtil# 创建窗口root = tk.Tk()root.title("系统资源监控")root.geometry("400x200")# 创建标签cpu_label = tk.Label(root, text="CPU占用率:", font=("Arial", 12))cpu_label.pack(pady=10)gpu_label = tk.Label(root, text="GPU占用率:", font=("Arial", 12))gpu_label.pack()temp_label = tk.Label(root, text="GPU温度:", font=("Arial", 12))temp_label.pack(pady=10)# 获取GPU信息gpus = GPUtil.getGPUs()if len(gpus) > 0: gpu = gpus[0]else: gpu = None# 更新标签信息def update_labels(): # 获取CPU信息 cpu_percent = psutil.cpu_percent() cpu_label.config(text="CPU占用率:{}%".format(cpu_percent)) # 获取GPU信息 if gpu is not None: gpu_percent = gpu.load*100 gpu_label.config(text="GPU占用率:{}%".format(gpu_percent)) gpu_temp = gpu.temperature temp_label.config(text="GPU温度:{}℃".format(gpu_temp)) # 每隔1秒更新标签信息 root.after(1000, update_labels)# 启动更新标签信息的函数update_labels()# 运行窗口root.mainloop()修改这段代码,不用psutil库和GPUtil库
时间: 2023-06-27 12:02:21 浏览: 48
import tkinter as tk
import os
# 创建窗口
root = tk.Tk()
root.title("系统资源监控")
root.geometry("400x200")
# 创建标签
cpu_label = tk.Label(root, text="CPU占用率:", font=("Arial", 12))
cpu_label.pack(pady=10)
gpu_label = tk.Label(root, text="GPU占用率:", font=("Arial", 12))
gpu_label.pack()
temp_label = tk.Label(root, text="GPU温度:", font=("Arial", 12))
temp_label.pack(pady=10)
# 更新标签信息
def update_labels():
# 获取CPU信息
cpu_percent = os.popen('top -bn1 | grep "Cpu(s)" | awk \'{print $2 + $4}\'').readline().strip()
cpu_label.config(text="CPU占用率:{}%".format(cpu_percent))
# 获取GPU信息
gpu_percent = os.popen('nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits | head -n 1').readline().strip()
gpu_label.config(text="GPU占用率:{}%".format(gpu_percent))
gpu_temp = os.popen('nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader,nounits | head -n 1').readline().strip()
temp_label.config(text="GPU温度:{}℃".format(gpu_temp))
# 每隔1秒更新标签信息
root.after(1000, update_labels)
# 启动更新标签信息的函数
update_labels()
# 运行窗口
root.mainloop()