Search code examples
pythonfunctionbuttontkinteros.system

Tkinter return result from os.system command for GUI


I've been playing around with Tkinter recently trying to execute a python script which works on its own. I have looked through a few other posts on the forum, and I can't seem to find a solution to what I'm playing with.

The following script, I do not have an issue with, this works perfectly.

import os
os.system('clear')

def check_ping():
    hostname = "8.8.8.8"
    # ping host once
    response = os.system("ping -c 1 " + hostname)
    # check the response...
    if response == 0:
        pingstatus = "\nYou are connected to the Internet. " + hostname + " is reachable"
    else:
        pingstatus = "\nNetwork Error - You are NOT connected to the Internet."

    return pingstatus

pingstatus = check_ping()
print(pingstatus)

On my Tkinter root window I have placed a label and two buttons, I want the label to display the status of the connection, or the result of the ping that is sent using os.system command.

The problem I have is that when this runs on startup, the label updates fine, When clicking on the button that calls the function, it does NOT update or return what I expect. Below is my code:

import os
from tkinter import *

root = Tk()
root.title('Ping Checker')

def check_ping():
hostname = "8.8.8.8"
response = os.system("ping -c 1 " + hostname)
# check the response...
if response == 0:
    pingstatus = "Internet Connected."
    icon = Label(root, text='Internet status: ' + pingstatus, bd=1)
else:
    pingstatus = "Network Error - NOT connected."
    icon = Label(root, text='Internet status: ' + pingstatus, bd=1)

return pingstatus

pingstatus = check_ping()

icon = Label(root, text='Internet status: ' + pingstatus, bd=1)
button_check = Button(root, text='Check Connection', command=check_ping)
button_quit = Button(root, text='Exit Program', command=root.quit)

icon.pack()
button_check.pack()
button_quit.pack()
root.mainloop()

I am trying to make a GUI interface for checking connections to different servers, eventually I would like to have this on a timer, so it can update automatically . Would some one please point me in the right direction, or explain why it works once on startup and not when clicking the button.

Thank you for your time.

Jozek


Solution

  • The problem is when you try to change the value of the label, you are simply changing what the variable icon actually is, you don't pack it into your window.

    A simple work around you can do for this is instead of redefining your icon, you could use the .config() method, which changes the details of a tkinter widget.

    For example

    def check_ping():
    hostname = "8.8.8.8"
    response = os.system("ping -c 1 " + hostname)
    # check the response...
    if response == 0:
        pingstatus = "Internet Connected."
        icon.config(text='Internet status: ' + pingstatus)
    else:
        pingstatus = "Network Error - NOT connected."
        icon.config(text='Internet status: ' + pingstatus)
    
    return pingstatus
    

    This should fix the problem you're having.

    Best of Luck,

    Ben