使用繁體中文,在w11下,使用python語言,UI界面,要如何設計出一個400*300畫面,使用者可以選擇本機電腦的網路卡,並將此網卡的相關資訊呈現在UI界面上
接上,要如何知道此網卡的連線速度,並呈現在UI界面上
接上,要如何知道此選擇的網卡的IP,並呈現在給他另一欄位,在UI界面上,單獨顯示IP的狀況
接上,IP的顯示,要包含IPV6
UDP-SENDER.EXE的參數有那些可以使用的?
import tkinter as tk
from tkinter import ttk
import psutil
def get_network_interfaces():
interfaces = psutil.net_if_addrs()
return [iface for iface in interfaces.keys()]
def update_info(event):
selected_iface = iface_combobox.get()
addrs = psutil.net_if_addrs()[selected_iface]
stats = psutil.net_if_stats()[selected_iface]
info_text.delete(1.0, tk.END) # 清空先前的資訊
for addr in addrs:
info_text.insert(tk.END, f"{addr.family.name}: {addr.address}\n")
# 顯示連線速度
info_text.insert(tk.END, f"連線速度: {stats.speed} Mbps\n")
info_text.insert(tk.END, f"是否啟用: {'是' if stats.isup else '否'}\n")
info_text.insert(tk.END, f"是否支援全雙工: {'是' if stats.duplex == 2 else '否'}\n")
# 建立主窗口
root = tk.Tk()
root.title("網路卡資訊")
root.geometry("400x300")
# 創建下拉選單
iface_label = tk.Label(root, text="選擇網路卡:")
iface_label.pack(pady=10)
iface_combobox = ttk.Combobox(root, values=get_network_interfaces())
iface_combobox.pack(pady=10)
iface_combobox.bind("<<ComboboxSelected>>", update_info)
# 創建顯示資訊的文本區域
info_label = tk.Label(root, text="網路卡資訊:")
info_label.pack(pady=10)
info_text = tk.Text(root, width=40, height=10)
info_text.pack(pady=10)
# 啟動主循環
root.mainloop()
在 psutil 模組中,AF_INET 和 AF_INET6 是用來表示地址家族的常數,但這些常數並不是直接由 psutil 提供的。你可以從 socket 模組中獲取這些常數。
請將程式碼中的 psutil.AF_INET 和 psutil.AF_INET6 更改為從 socket 模組獲取,以下是更新後的程式碼:
import tkinter as tk
from tkinter import ttk
import psutil
import socket # 新增引入 socket 模組
def get_network_interfaces():
interfaces = psutil.net_if_addrs()
return [iface for iface in interfaces.keys()]
def update_info(event):
selected_iface = iface_combobox.get()
addrs = psutil.net_if_addrs()[selected_iface]
stats = psutil.net_if_stats()[selected_iface]
# 清空先前的資訊
info_text.delete(1.0, tk.END)
ip_text.delete(1.0, tk.END)
# 顯示連線速度和狀態
info_text.insert(tk.END, f"連線速度: {stats.speed} Mbps\n")
info_text.insert(tk.END, f"是否啟用: {'是' if stats.isup else '否'}\n")
info_text.insert(tk.END, f"是否支援全雙工: {'是' if stats.duplex == 2 else '否'}\n")
# 顯示 IP 地址(包括 IPv4 和 IPv6)
for addr in addrs:
if addr.family == socket.AF_INET: # 使用 socket 模組的常數
ip_text.insert(tk.END, f"IPv4 地址: {addr.address}\n")
elif addr.family == socket.AF_INET6: # 使用 socket 模組的常數
ip_text.insert(tk.END, f"IPv6 地址: {addr.address}\n")
# 建立主窗口
root = tk.Tk()
root.title("網路卡資訊")
root.geometry("400x400")
# 創建下拉選單
iface_label = tk.Label(root, text="選擇網路卡:")
iface_label.pack(pady=10)
iface_combobox = ttk.Combobox(root, values=get_network_interfaces())
iface_combobox.pack(pady=10)
iface_combobox.bind("<<ComboboxSelected>>", update_info)
# 創建顯示其他資訊的文本區域
info_label = tk.Label(root, text="網路卡狀態:")
info_label.pack(pady=10)
info_text = tk.Text(root, width=40, height=5)
info_text.pack(pady=10)
# 創建顯示 IP 的文本區域
ip_label = tk.Label(root, text="IP 地址:")
ip_label.pack(pady=10)
ip_text = tk.Text(root, width=40, height=3)
ip_text.pack(pady=10)
# 啟動主循環
root.mainloop()