Python Forum
[Tkinter] development of network equipment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] development of network equipment
#1
Hi programmers, I need some advice on the application I am developing in python. It is an application that will be something like a network device that will include ping, traceroute, iperf, tshark, static IP address settings, DHCP, ARP table invocation, etc. and then implemented on RPi. Invoking these outputs is not so complicated, but the problem arises in developing UIs to be clear and simple for the user.
To give you a little insight into what my problem is.
If the user wants to ping an IP address, he / she will see a form in which he / she will write the IP address and after confirmation the output ping will show up on the right side of the application in real time. It all works fine.
However, the problem arises when I want to click on another function, such as traceroute, and do the same thing as the previous one. I will see 2 forms below each other and the output ping is still in place on the right.

I tried: I tried different conditions that could close the previous frames and listboxes so that the new selected form could be displayed. However, I did not succeed.

I want to achieve: As I mentioned, I am trying to close the previously used function and replace it with another function the user chooses.

Here is the source code:

from tkinter import *
import tkinter as tk
import os
import subprocess
from threading import Thread
import string
import time

#FORMS

#form for ip static
def ip_static():
        global screen1
        screen1 = Toplevel(root)
        screen1.title("setting static ip address")
        screen1.geometry("400x280")

        global ip
        global netmask
        global gateway

        ip = StringVar()
        netmask = StringVar()
        gateway = StringVar()

        Label(screen1, text="ip address:").pack()
        ip_entry = Entry(screen1, textvariable = ip)
        ip_entry.pack()

        Label(screen1, text="netmask:").pack()
        netmask_entry = Entry(screen1, textvariable = netmask)
        netmask_entry.pack()

        Label(screen1, text="broadcast:").pack()
        gateway_entry = Entry(screen1, textvariable = gateway)
        gateway_entry.pack()

        Label(screen1, text="").pack()
        Button(screen1, text = "set", width = 10, height = 1, command = ip_static_save).pack()
        Label(screen1, text="").pack()
        Button(screen1, text = "close", width = 10, height = 1, command = screen1.destroy).pack()

        screen1.transient(root)
        screen1.grab_set()
        root.wait_window(screen1)



#form for ping

def ping():
        window = Frame(ctr_left, width=200, height=200)
        window.grid(padx=40, pady=45)

        global ip_pinging
        ip_pinging = StringVar()

        Label(root, text="ip address for ping:").grid(in_=window)
        ip_entry = Entry(root, textvariable = ip_pinging)
        ip_entry.grid(in_=window)

        Label(root, text="").grid(in_=window)
        Button(root, text = "ping", width = 10, height = 1, command =lambda: create_worker(ip_ping)).grid(in_=window)


def traceroute():
        window = Frame(ctr_left, width=200, height=200)
        window.grid(padx=40, pady=45)

        global ip_tr
        ip_tr = StringVar()

        Label(root, text="ip address for traceroute:").grid(in_=window)
        ip_entry = Entry(root, textvariable = ip_tr)
        ip_entry.grid(in_=window)


        Label(root, text="").grid(in_=window)
        Button(root, text = "traceroute", width = 10, height = 1, command =lambda: create_worker(ip_traceroute)).grid(in_=window)

#FUNCTIONS

#function for set static IP

def create_worker(name):
        our_thread = Thread(target=name)
        if not our_thread.is_alive():
                our_thread.start()

def ip_static_save():
        ip_val = ip.get()
        netmask_val = netmask.get()
        gateway_val = gateway.get()
        os.system('echo '+ip_val+' '+netmask_val+' '+gateway_val+'>> dhcpcd.log')
        screen1.destroy()

def ip_ping():
        window = Frame(ctr_right, width=200, height=200)
        window.grid(padx=20, pady=30)

        scroll = Scrollbar(window)
        scroll.pack(side=RIGHT, fill=Y)

        listbox = Listbox(window, width=90, height=20, yscrollcommand=scroll.set)
        listbox.pack(side=LEFT)

        scroll.config(command=listbox.yview)

        ip_val = ip_pinging.get()
        text = ""
        row = 1
        ping = subprocess.Popen(["ping", ip_val.strip()], stdout=subprocess.PIPE, shell=True)
        lbl = tk.Label(root)
        while True:
                output = ping.stdout.readline()
                decoding = output.decode("utf-8")
                clean_output = decoding.rstrip("\n")
                if clean_output == '':
                        break
                if clean_output:
                        listbox.insert(END, clean_output)


def ip_traceroute():
        window = Frame(ctr_right, width=200, height=200)
        window.grid(padx=20, pady=30)

        scroll = Scrollbar(window)
        scroll.pack(side=RIGHT, fill=Y)

        listbox = Listbox(window, width=90, height=20, yscrollcommand=scroll.set)
        listbox.pack(side=LEFT)

        scroll.config(command=listbox.yview)

        ip_val = ip_tr.get()
        text = ""
        row = 0
        tr = subprocess.Popen(["tracert", ip_val.strip()], stdout=subprocess.PIPE, shell=True)
        lbl = tk.Label(root)
        while True:
                output = tr.stdout.readline()
                decoding = output.decode("utf-8")
                clean_output = decoding.rstrip("\n")
                if clean_output == '':
                        break
                if clean_output:
                        listbox.insert(END, clean_output)
#MAIN SCREEN

root = tk.Tk()
root.geometry('800x480')
root.title("network tester")
root.resizable(width=False, height=False)

top_frame = Frame(root, width=100, height=20, pady=3)
center = Frame(root, width=100, height=40,pady=3)

root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)

top_frame.grid(row=0, sticky="ew")
center.grid(row=1, sticky="nsew")

center.grid_rowconfigure(0, weight=1)
center.grid_columnconfigure(1, weight=1)

ctr_left = Frame(center, width=200, height=430)
ctr_right = Frame(center, width=600, height=430, padx=3, pady=3)
ctr_left.grid_propagate(0)
ctr_right.grid_propagate(0)

ctr_left.grid(row=0, column=0, sticky="ns")
ctr_right.grid(row=0, column=2, sticky="ns")



#MENUBUTTONS AND BUTTONS

#network settings
menubutton = Menubutton(top_frame, text = "network settings", width = 22, height = 2, relief=RAISED, bd=2)
menubutton.grid(padx=5, pady=4, column=0, row=0)

menubutton.menu = Menu(menubutton, tearoff=0)
menubutton["menu"] = menubutton.menu

menubutton.menu.add_command(label="static IP", command=ip_static)
menubutton.menu.add_command(label="DHCP")
menubutton.menu.add_separator()
menubutton.menu.add_command(label="close")
menubutton.grid()

#network testing
menubutton = Menubutton(top_frame, text = "network testing", width = 22, height = 2, relief=RAISED, bd=2)
menubutton.grid(padx=5, pady=4, column=1, row=0, rowspan=1)

menubutton.menu = Menu(menubutton, tearoff=0)
menubutton["menu"] = menubutton.menu

menubutton.menu.add_command(label="ping", command=ping)
menubutton.menu.add_command(label="traceroute", command=traceroute)
menubutton.menu.add_separator()
menubutton.menu.add_command(label="close")
menubutton.grid()


button = tk.Button(top_frame, text = "informace", width = 20, height = 2)
button.grid(padx=5, pady=5, column=2, row=0)

button = tk.Button(top_frame, text = "nástroje", width = 20, height = 2)
button.grid(padx=5, pady=5, column=3, row=0)

button = tk.Button(top_frame, text = "report", width = 20, height = 2)
button.grid(padx=5, pady=5, column=4, row=0)

root.mainloop()
If I have forgotten something, I am very sorry and I will explain anything if it is important to solve my problem.
Thank you
Reply
#2
You can do it all in tkinter, but the effort involved will be much greater that if you use wxpython, which is so much easier to use, and comes with a huge demo program that has examples and code for just about every widget that the package offers.
Suggest trying it out
You can usually install the package with pip install wxpython
then get a copy of the demo (don't need the entire package) here: https://github.com/wxWidgets/Phoenix
and try it out. I think you'll be pleasantly surprised.
There are several things about tkinter that I don't like. The worst is it's geography which is difficult at best to get working properly. I also don't think the look is very modern, but that's just my opinion.
there is a crude but effective gallery here: https://wxpython.org/Phoenix/docs/html/gallery.html
Reply
#3
I have no output on the ping or trace route- the list box is created but nothing in it.
Make a class and instead of a Listbox use the Text widget outside the function with a name. You can clear it at the start of each function. To remove a widget you can use pack_foreget(). I think you're making a new Listbox every time you push the button. The use of while True: what's stops this thread? While True is forever is that what you want?
Reply


Forum Jump:

User Panel Messages

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