[英]python tkinter menu refresh
I have created a python script that connects to my homemade nas with ssh and showing the size, used and available of the hdd in a tkinter menu by sending a command and printing it:我创建了一个python脚本,它使用 ssh 连接到我自制的 nas 并通过发送命令并打印它在tkinter菜单中显示硬盘的大小、已用和可用:
import sys, paramiko
from tkinter import *
import os
import tkinter as tk
username = "user"
hostname = "ip"
password = "pass"
command = "df /dev/sda3 -h"
port = 22
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.connect(hostname, port=port, username=username, password=password)
stdin, stdout, stderr = client.exec_command(command)
output = stdout.read()
root = Tk()
root.minsize(width=425, height=200)
root.maxsize(width=425, height=200)
root.configure(background='blue')
menubar = Menu(root)
root.title("NAS storage")
msg = tk.Message(root, text = ("size", output[65:69], "used", output[72:75], "available", output[77:81])) #should output hdd info (size, used, avalable)
msg.config(bg='lightgreen', font=('times', 50, 'italic'))
msg.pack()
tk.mainloop()
root.config(menu=menubar)
root.mainloop()
finally:
client.close()
It works but I want to have it refreshed every 5 seconds.它有效,但我希望每 5 秒刷新一次。
There's no need to constantly refresh the menu.无需不断刷新菜单。 You can specify a command to run immediately before the menu is displayed with the postcommand
option.您可以使用postcommand
选项指定在菜单显示之前立即运行的命令。
Here is a contrived example that uses the current time as an item on the menu.这是一个人为的例子,它使用当前时间作为菜单上的一个项目。 Every time you show the menu it will show a new time.每次显示菜单时,它都会显示一个新时间。
import tkinter as tk
import time
def updateTimeMenu():
time_menu.delete(0, "end")
time_menu.add_command(label=time.ctime())
root = tk.Tk()
root.geometry("400x200")
menubar = tk.Menu(root)
root.configure(menu=menubar)
time_menu = tk.Menu(menubar, postcommand=updateTimeMenu,
tearoff=False)
menubar.add_cascade(label="Time", menu=time_menu)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.