簡體   English   中英

每 1 秒用新信息更新 tkinter window

[英]Update every 1 second the tkinter window with new information

我想讓這個小應用程序每 1 秒更新一次來自tkinter window的信息。 The application shows computer info and generates a.txt file and then parses that into a tkinter window, which is the first issue with the app, which will not help if I want to update the contents of the tkinter windows every 1 second, since I還需要先更新.txt文件。

我創建了一個 class (UpdateTextTkinter) 只是為了測試我是否可以通過按下按鈕從 window 更新文本 - 我正在考慮每次按下按鈕時使用它來更新內容。

我需要什么幫助或想法:

  1. 如何從.txt 遷移到 tkinter window 以僅生成 tkinter Z05B8C74CBD96FBF2DE4C1A352702FBF4 並將文本附加到它?
  2. 如何每 1 秒用新信息更新 window?

這是代碼:

# check if file exists, if not create with WRITE flag
path_text = pathlib.Path("path") # path to txt file
if path_text.is_file():
    # make file WRITABLE
    filename = "path" # path to txt file
    os.chmod(filename, S_IWUSR)
else:
    # pop-up: file not found and will be created
    response = win32ui.MessageBox("File inexistent - will be created", "Exception: File not found", win32con.MB_OKCANCEL)
    if response == win32con.IDCANCEL:
        # branch to exit execution if CANCEL is pressed
        exit()
    # branch to continue execution if OK is pressed
    elif response == win32con.IDOK:
        # file was not found, create it here and apprend it with data (write flag)
        f = open("path", "w+") #path to txt file
# covert large numbers of bytes into a defined format (kilo, mega, giga, tera, peta)
def get_size(bytes, suffix="B"):
    """
    Scale bytes to proper format:

    1253656 -> '1.20MB'
    1253656678 -> '1.17GB'
    :param bytes:
    :param suffix:
    :return:
    """
    factor = 1024
    for unit in ["", "K", "M", "G","T", "P"]:
        if bytes < factor:
            return f"{bytes:.2f}{unit}{suffix}"
        bytes /= factor


# class UpdateTextTkinter:
#     def __init__(self):
#         self.root = tk.Tk()
#         self.text = tk.StringVar()
#         # pass object to be updated
#         self.text.set("Test")
#         self.label = tk.Label(self.root, textvariable=self.text)
#
#         self.button = tk.Button(self.root,
#                                 text = "UPDATE",
#                                 command = self.changeText)
#         self.button.pack()
#         self.label.pack()
#         self.root.mainloop()
#
#     # define changeText function and pass object to be updated
#
#     def changeText(self):
#         self.text.set("Test u")
#
#
# app = UpdateTextTkinter()

# Reminder
print("="*40, "REMINDER", "="*40, file = f)
now_time = datetime.now()
current_time = now_time.strftime("%H:%M:%S")
print(f"Keep in mind that this is not updating live, it's just a snapshot of the system at a certain time({current_time})!", file = f)


# Sys info
print("\n", "="*40, "System Information", "="*40, file = f)
uname = platform.uname()
print(f"System: {uname.system}", file = f)
print(f"Node Name: {uname.node}", file = f)
print(f"Release: {uname.release}", file = f)
print(f"Version: {uname.version}", file = f)
print(f"Machine: {uname.machine}", file = f)
print(f"Processor: {uname.processor}", file = f)


# Get boot date and time
print("\n", "="*40, "BOOT Time", "="*40, file = f)
boot_time_timestamp = psutil.boot_time()
bt = datetime.fromtimestamp(boot_time_timestamp)
print(f"Boot date: {bt.day}/{bt.month}/{bt.year}", file = f)
print(f"Boot time: {bt.hour}/{bt.minute}/{bt.second}", file = f)


# CPU info
print("\n", "="*40, "CPU Info", "="*40, file = f)

# Get number of Cores
print("Physical cores: ",psutil.cpu_count(logical=False), file = f)
print("Threads: ",psutil.cpu_count(logical=True), file = f)

# CPU frequencies
cpufrequency = psutil.cpu_freq()
print(f"Mininum frequency: {cpufrequency.min:.2f}MHz", file = f)
print(f"Maximum frequency: {cpufrequency.max:.2f}MHz", file = f)
print(f"Current frequency: {cpufrequency.current:.2f}MHz", file = f)

# CPU usage
print("CPU Usage Per Core:", file = f)
for i, percentage in enumerate(psutil.cpu_percent(percpu = True, interval = 1)):
    print(f"Core {i}: {percentage}%", file = f)
print(f"Total CPU Usage: {psutil.cpu_percent()}%", file = f)


# MEM information
print("\n", "="*40, "Memory Information", "="*40, file = f)

# get memory details
svmem = psutil.virtual_memory()
print(f"Total memory: {get_size(svmem.total)}", file = f)
print(f"Available memory: {get_size(svmem.available)}", file = f)
print(f"Used memory: {get_size(svmem.used)}", file = f)
print(f"Percentage memory: {svmem.percent}%", file = f)

# get swap memory (if existent)
swap = psutil.swap_memory()
print(f"Total swap memory: {get_size(swap.total)}", file = f)
print(f"Free swap memory: {get_size(swap.free)}", file = f)
print(f"Used swap memory: {get_size(swap.used)}", file = f)
print(f"Percentage swap memory: {swap.percent}%", file = f)


# DISK usage
print("\n", "="*40, "Disk Information", "="*40, file = f)
print("Partitions and usage:", file = f)

# get all disk partitions
partitions = psutil.disk_partitions()
for partitions in partitions:
    print(f"\nDevice: {partitions.device}", file = f)
    print(f"Mountpoint: {partitions.mountpoint}", file = f)
    print(f"File sys type: {partitions.fstype}", file = f)
    try:
        partitions_usage = psutil.disk_usage(partitions.mountpoint)
    except PermissionError:
        # can't be catched due to disk that isn't ready
        continue
    print(f"Total size: {get_size(partitions_usage.total)}", file = f)
    print(f"Used space: {get_size(partitions_usage.used)}", file = f)
    print(f"Free space: {get_size(partitions_usage.free)}", file = f)
    print(f"Percentage size: {partitions_usage.percent}%", file = f)

# get IO statistics since boot
disk_io = psutil.disk_io_counters()
print(f"Total read: {get_size(disk_io.read_bytes)}", file = f)
print(f"Total write: {get_size(disk_io.write_bytes)}", file = f)


# Network information
print("\n", "="*40, "Network Information", "="*40, file = f)
# get all network interfaces (virtual and physical)
if_addrs = psutil.net_if_addrs()
for interface_name, interface_addresses in if_addrs.items():
    for address in interface_addresses:
        print(f"=== Interface: {interface_name} ===", file = f)
        if str(address.family) == 'AddressFamily.AF_INET':
            print(f"  IP Address: {address.address}", file = f)
            print(f"  Netmask: {address.netmask}", file = f)
            print(f"  Broadcast IP: {address.broadcast}", file = f)
        elif str(address.family) == 'AddressFamily.AF_PACKET':
            print(f"  MAC Address: {address.address}", file = f)
            print(f"  Netmask: {address.netmask}", file = f)
            print(f"  Broadcast MAC: {address.broadcast}", file = f)
# get IO statistics since boot
net_io = psutil.net_io_counters()
print(f"Total Bytes Sent: {get_size(net_io.bytes_sent)}", file = f)
print(f"Total Bytes Received: {get_size(net_io.bytes_recv)}", file = f)


# Make file READ-ONLY
# filename = "path" #path to txt file
# os.chmod(filename, S_IREAD)

#Close file
f.close()

# Import text file and parse it into a tkinter GUI
# filepath = "path" #path to txt file

root = Tk()
root.title("Computer Informations")
root.iconbitmap("C:\\Users\\User\\PycharmProjects\\FirstSteps\\Computer_info\\iconbitmap")
# open file as in Tkinter (read-only)
with open("path", "r") as f: # path to txt file
    # display file in a tkinter window
    Label(root, text=f.read()).pack()

root.mainloop()

# Delete file after is parsed in Tkinter
if os.path.exists("path"): # path to txt file
    os.remove("path") # path to txt file
else:
    # create message box error: file not found, cannot be deleted
    exit_response = win32ui.MessageBox("File inexistent - program will exit", "Exception: File not found", win32con.MB_OK)
    # branch to continue exectuion if OK is pressed
    if exit_response == win32con.IDOK:
        exit()

PS:顯示sys-info的代碼來自網絡,我正在嘗試通過使用它來了解更多Python!

謝謝你!

對於每隔一段時間更新 window,您可以使用如下線程:

from threading import Thread
from time import sleep

def update():
    while True:
        # do what you want to update here
        sleep(1)

# and to start the thread, just do
Thread(target=update).start()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM