如何循环这段代码? [英] How to loop this code?

查看:26
本文介绍了如何循环这段代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图循环这个,但每次都失败.这是我尝试循环的 def create_widgets.所以我得到了一个 GUI,它会在某些东西离线时显示一个红色按钮/框.

这是我尝试使用的代码.

from tkinter import *类应用程序(框架):"""图形用户界面"""def __init__(self, master):"""初始化框架"""Frame.__init__(self,master)self.grid()self.create_widgets()def create_widgets(self):"""创建按钮."""导入操作系统#路由器self.button1 = 按钮(自我)self.button1["text"] = "路由器"self.button1["fg"] = "白色"self.button1.grid(padx=0,pady=5)#平主机名 = "10.18.18.1"response = os.system("ping -n 1 " + 主机名)#回复如果响应 == 0:self.button1["bg"] = "绿色"别的:self.button1["bg"] = "红色"根 = Tk()root.title("监视器")root.geometry("500x500")应用程序 = 应用程序(根)root.mainloop()

解决方案

您可以使用 Tkafter<将其附加到 Tk 的事件循环中/code> 方法.

def if_offline(self):#平主机名 = "10.18.18.1"response = os.system("ping -n 1 " + 主机名)#回复如果响应 == 0:self.button1["bg"] = "绿色"别的:self.button1["bg"] = "红色"

然后,此行位于 app = Application(root)root.mainloop() 之间的任何位置:

root.after(0, app.if_offline)

after 将进程附加到 Tk 事件循环上.第一个参数是以毫秒为单位重复该过程的频率,第二个参数是要执行的函数对象.由于我们指定的时间为0,它会不断检查并不断更新按钮的背景颜色.如果这会影响您的 CPU,或者您不想 ping 那么多,您可以将重复时间更改为更大的值.

传入的函数对象应该就是:一个函数对象.它与 Button 构造函数中的命令参数具有相同的规则.如果您需要将参数传递给函数,请使用如下所示的 lambda:

root.after(0, lambda: function(argument))

这是可行的,因为 lambda 函数返回一个函数对象,该对象在执行时将运行 function(argument).

来源

I have trying to loop this but fail everytime. It´s the def create_widgets I try to loop. So I got a GUI that show a red button/box as soon something goes offline.

This is the code I tried to use.

from tkinter import *

class Application(Frame):
""" GUI """

def __init__(self, master):
    """ Initialize the Frame"""
    Frame.__init__(self,master)
    self.grid()
    self.create_widgets()

def create_widgets(self):
    """Create button. """
    import os
    #Router
    self.button1 = Button(self)
    self.button1["text"] = "Router"
    self.button1["fg"] = "white"
    self.button1.grid(padx=0,pady=5)
    #Ping
    hostname = "10.18.18.1"
    response = os.system("ping -n 1 " + hostname)
    #response
    if response == 0:
        self.button1["bg"] = "green"
    else:
        self.button1["bg"] = "red"

root = Tk()
root.title("monitor")
root.geometry("500x500")

app = Application(root)

root.mainloop()

解决方案

You can attach it onto Tk's event loop using Tk's after method.

def if_offline(self):
    #Ping
    hostname = "10.18.18.1"
    response = os.system("ping -n 1 " + hostname)
    #response
    if response == 0:
        self.button1["bg"] = "green"
    else:
        self.button1["bg"] = "red"

Then, this line goes anywhere between app = Application(root) and root.mainloop():

root.after(0, app.if_offline)

after attaches a process onto the Tk event loop. The first argument is how often the process should be repeated in milliseconds, and the second is the function object to be executed. Since the time we have specified is 0, it will constantly check and constantly update the button's background color. If that churns your CPU, or you don't want to be pinging that much, you can change the repeat time to something larger.

The function object passed in should be just that: a function object. It has the same rules as the command argument in a Button constructor. If you need to pass in arguments to the function, use a lambda like so:

root.after(0, lambda: function(argument))

This works because the lambda function returns a function object, which when executed will run function(argument).

Source

这篇关于如何循环这段代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆