Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.
Avatar
Jakub Štolba:23.12.2019 14:35

Ahoj programátoři, potřeboval bych poradit ohledně aplikace, kterou vyvíjím v pythonu. Jedná se o aplikaci, která bude něco jako zařízení pro síť, která bude obsahovat funkce ping, traceroute, iperf, tshark, nastavení statické IP adresy, DHCP, vyvolání ARP tabulky atd. a následně bude implementována na RPi. Vyvolání těchto výstupů není až tak složité, ale problém nastává u vývoji UI aby bylo přehledné a jednoduché pro uživatele.
Abych Vás malinko zasvětil, co je můj problém.
Pokud uživatel bude chtít pingovat nějakou IP adresu, tak se mu objeví form, do kterého vypíše IP adresu a po potvrzení se output pingování ukáže na pravé straně aplikace v real timu. To vše funguje v pořádku.
Problém však nastává, když chci rozkliknout jinou funkci, např. traceroute a udělat stejný postup jako u předešlé funkce. zobrazí se mi 2 formy pod sebou a output pingu je pořád na svém místě vpravo viz Obrázek3.png.

Zkusil jsem: Zkoušel jsem různé podmínky, které by dokázali zavřít předešlé framy a listboxy, aby se mohl zobrazit nový zvolený form. To se mi však nepovedlo.

Chci docílit: Jak už jsem zmínil, snažím se zavřít předešlou využitou funkci a nahradit ji jinou funkcí, kterou si uživatel zvolí.

Zde přikládám zdrojový kód:

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()

Pokud jsem na něco zapomněl, tak se velice omlouvám a cokoliv dovysvětlím, pokud to bude důležité pro vyřešení mého problému.
Děkuji

 
Odpovědět
23.12.2019 14:35
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 1 zpráv z 1.