0

I'm not being able to make this line work with Tk

import os
while(1):
    ping = os.popen('ping www.google.com -n 1')
    result = ping.readlines()
    msLine = result[-1].strip()
    print msLine.split(' = ')[-1]

I'm trying to create a label and text = msLine.split... but everything freezes

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Bruno 'Shady'
  • 4,348
  • 13
  • 55
  • 73
  • Your above code works perfectly for me. – Paul Mar 12 '10 at 05:33
  • Duplicate of http://stackoverflow.com/questions/2430519/how-to-ping-an-ip-and-get-only-the-ms-in-the-tk-with-python ? Edit: maybe not finally .. not sure. –  Mar 12 '10 at 05:44
  • He (?) took the answer from that question and made a new question. I think this is less of a "how do I do this" question and more of a "do the work for me" question. – Bryan Oakley Mar 13 '10 at 14:06

2 Answers2

0

There can be other issues with Tk and popen(). First:

Thou shalt not continously ping or fetch from google.com.

Add a "import time" at the top and "time.sleep(2)" at the bottom of the while loop.

Second:

You probably meant "ping www.google.com -c 1" instead of "-n 1". The "-c 1" asks for one ping only. The "-n 1" pings 0.0.0.1.

Charles Merriam
  • 19,908
  • 6
  • 73
  • 83
0

Your example code shows no GUI code. It is impossible to guess why your GUI freezes without seeing the code. Though, your code is pretty buggy so even if there were GUI code in your post it likely wouldn't help.

Is it possible that you're forgetting to call the mainloop() method on your root widget? That would explain the freeze. And if you are calling mainloop(), there's no reason to do while(1) since the main event loop itself is an infinite loop. Why are you calling ping in a loop?

One specific problem you have is that you are calling ping wrong. For one, the option "-n 1" needs to come before the hostname argument (ie: 'ping -n 1 www.google.com' instead of 'ping www.google.com -n 1'). Also, -n is the wrong thing to do. I think you want "-c 1"

Here's a working example of how you can ping periodically and update a label:

import os
from Tkinter import *
class App:
    def __init__(self):
        self.root = Tk()
        self.create_ui()
        self.url = "www.google.com"
        self.do_ping = False
        self.root.mainloop()

    def create_ui(self):
        self.label = Label(self.root, width=32, text="Ping!")
        self.button = Button(text="Start", width=5, command=self.toggle)
        self.button.pack(side="top")
        self.label.pack(side="top", fill="both", expand=True)

    def toggle(self):
        if self.do_ping:
            self.do_ping = False
            self.button.configure(text="Start")
        else:
            self.do_ping = True
            self.button.configure(text="Stop")
            self.ping()

    def ping(self):
        if not self.do_ping:
            return
        ping = os.popen('ping -c 1 %s' % self.url)
        result = ping.readlines()
        msLine = result[-1].strip()
        data = msLine.split(' = ')[-1] 
        self.label.configure(text=data)
        # re-schedule to run in another half-second
        if self.do_ping:
            self.root.after(500, self.ping)

app=App()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685