简体   繁体   English

Tkinter 重叠网格小部件

[英]Tkinter overlapping grid widgets

everyone.每个人。 I'm new to Python and trying to learn it as my future jobs will require me knowing it.我是 Python 的新手并试图学习它,因为我未来的工作需要我了解它。 I'm playing around with Tkinter, trying to get a pinging script to work.我在玩 Tkinter,试图让 ping 脚本工作。 The result of this script will show a list of servers in column 0 and a list of whether it is up or down in column 1. I have it working except for one thing: the widgets overlap, causing this script to be a memory hog.此脚本的结果将显示第 0 列中的服务器列表和第 1 列中它是向上还是向下的列表。我让它工作,除了一件事:小部件重叠,导致此脚本成为内存猪。 For example, if the site "google.com" responds with "UP" and I take down my internet, it will show as "DOWN".例如,如果站点“google.com”响应为“UP”而我关闭了我的互联网,它将显示为“DOWN”。 However, as soon as a plug my internet back in, it will show as "UP" but I can see the remnants of the word "DOWN" behind the label.但是,只要重新插入我的互联网,它就会显示为“UP”,但我可以看到标签后面的“DOWN”字样。 I've tried different ways to destroy the widget before every utilization but can not get it to work.在每次使用之前,我尝试了不同的方法来销毁小部件,但无法使其正常工作。 I understand if my code is a little messy so I'm definitely open to criticism.我理解我的代码是否有点乱,所以我绝对愿意接受批评。 Below is the code I have with a few example sites listed in the "host" variable:下面是我在“host”变量中列出的一些示例站点的代码:

import pyping
import Tkinter as tk
from Tkinter import *
import time

host = ["google.com", "yahoo.com", "espn.com"]

root = tk.Tk()

class PingTest:

    result = []
    resultfc = []

    def __init__(self, hostname, inc):
        self.hostname = hostname
        self.inc = inc
        self.ping(hostname)

    def results(self, result1, resultfc1):
        self.result = result1
        self.resultfc = resultfc1

    def ping(self, y):
        self.y = y
        q = ""
        try:
            x = pyping.ping(self.y, count=1)
            q = x.ret_code
        except Exception:
            pass
        finally:
            if q == 0:
                self.results("UP", "green")
            else:
                self.results("DOWN", "red")

        self.window()

    def window(self):

        self.label1 = Label(root, text=self.hostname)
        self.label2 = Label(root, text=self.result, fg=self.resultfc, bg="black")

        a = Label(root, text=self.hostname)
        b = Label(root, text=self.result, fg=self.resultfc, bg="black")
        b.update()
        b.update_idletasks()
        if b == TRUE:
            b.grid_forget() # These two lines don't seem to help my cause
            b.destroy()
        a.grid(row=self.inc, column=0)
        b.grid(row=self.inc, column=1)


while TRUE:
    i = 0
    for h in host:
        PingTest(h, i)
        i += 1
    time.sleep(1)

I would update labels instead of destroying them.我会更新标签而不是销毁它们。

We can use threading to check each site without having to block the mainloop() .我们可以使用线程检查每个站点而不必阻塞mainloop() By creating a list of labels you can use the index of the list to set up the labels on your GUI and at the same time we can start a thread per object in list to check on the site status and return if site is up or down.通过创建标签列表,您可以使用列表的索引在 GUI 上设置标签,同时我们可以为列表中的每个对象启动一个线程来检查站点状态并返回站点是启动还是关闭. I chose to use urllib and threading to make this work.我选择使用urllibthreading来完成这项工作。

import tkinter as tk
import urllib.request
import threading
import time

host = ["google.com", "yahoo.com", "espn.com"]

class CheckURL:
    def __init__(self, host, widget):
        self.host = host
        self.widget = widget
        self.update_labels()

    def update_labels(self):
        if urllib.request.urlopen("http://www." + self.host).getcode() == 200:
            self.widget.config( text='UP', fg='green')
        else:
            self.widget.config(text='DOWN', fg='red')
        time.sleep(5)
        self.update_labels()

root = tk.Tk()
labels = []

for ndex, x in enumerate(host):
    tk.Label(root, text=x).grid(row=ndex, column=0)
    labels.append(tk.Label(root, text='DOWN', fg='red'))
    labels[-1].grid(row=ndex, column=1)
    threading._start_new_thread(CheckURL, (x, labels[-1]))

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM