Python Forum
[Tkinter] How to get the result of a ping to show in tkinter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to get the result of a ping to show in tkinter?
#2
This is quite a tricky task as calling ping can block the GUI mainloop.
The code below uses the technique from the following forum tutorial.
https://python-forum.io/Thread-Tkinter-H...ng-the-gui

import functools
import os
import tkinter as tk
from concurrent import futures

thread_pool_executor = futures.ThreadPoolExecutor(max_workers=1)


def tk_after(target):

    @functools.wraps(target)
    def wrapper(self, *args, **kwargs):
        args = (self,) + args
        self.after(0, target, *args, **kwargs)

    return wrapper


def submit_to_pool_executor(executor):

    def decorator(target):

        @functools.wraps(target)
        def wrapper(*args, **kwargs):
            return executor.submit(target, *args, **kwargs)

        return wrapper

    return decorator


class MainFrame(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.master.geometry('500x350')
        self.master.title("Get output inside GUI")
        self.entry = tk.StringVar()
        label = tk.Label(
            self.master, text="Enter target IP or host as required.")
        label.pack()
        entry = tk.Entry(self.master, textvariable=self.entry)
        entry.insert(-1, "8.8.8.8")
        entry.pack()
        self.button = tk.Button(
            self.master, text="Ping Test", command=self.on_button)
        self.button.pack()
        self.text = tk.Text(self.master)
        self.text.config(state=tk.DISABLED)
        self.text.pack(padx=5, pady=5)

    @tk_after
    def button_state(self, enabled=True):
        state = tk.NORMAL
        if not enabled:
            state = tk.DISABLED
        self.button.config(state=state)

    @tk_after
    def clear_text(self):
        self.text.config(state=tk.NORMAL)
        self.text.delete(1.0, tk.END)
        self.text.config(state=tk.DISABLED)

    @tk_after
    def insert_text(self, text):
        self.text.config(state=tk.NORMAL)
        self.text.insert(tk.END, text)
        self.text.config(state=tk.DISABLED)

    def on_button(self):
        self.ping()

    @submit_to_pool_executor(thread_pool_executor)
    def ping(self):
        self.button_state(False)
        self.clear_text()
        self.insert_text('Starting ping request')

        result = os.popen("ping "+self.entry.get()+" -n 2")
        for line in result:
            self.insert_text(line)

        self.insert_text('ping request finished')
        self.button_state(True)


if __name__ == '__main__':
    app = tk.Tk()
    main_frame = MainFrame()
    app.mainloop()
NebularNerd likes this post
Reply


Messages In This Thread
RE: How to get the result of a ping to show in tkinter? - by Yoriz - Jun-09-2019, 04:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to add two numbers and pass the result to the next page in tkinter? pymn 7 4,518 Feb-15-2022, 04:40 AM
Last Post: pymn
  Show the result of every count George87 10 2,874 Dec-28-2021, 10:03 PM
Last Post: deanhystad
  [Tkinter] Result not change using Tkinter cmala 2 2,836 May-17-2019, 08:12 AM
Last Post: cmala
  [Tkinter] How to show and hide tkinter entry box when select yes from drop down Prince_Bhatia 1 10,629 Jun-12-2018, 08:05 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020