Python - Ayudaaa interfaz tkinter y subprocess.run

 
Vista:
Imágen de perfil de Alejandro

Ayudaaa interfaz tkinter y subprocess.run

Publicado por Alejandro (7 intervenciones) el 29/01/2023 22:10:07

Hola amigos dejo este video para que me entiendan mejor sobre el problema que tengo, en resumen intento usar un subprocess.run para enviar un ping a una direccion ip, si el processo se puede completar retorna True y vuelve a enviar un ping y asi sucesivamente, cuando el ping es exitoso trato de poner color verde un label como referencia visual para el usuario, pero esto no ocurre, simplemente se estan haciendo los pings pero como que la interfaz de traba y bloquea hasta que termina de hacer los pings y con esto pues no puedo agregar un boton de stop por ejemplo como para parar el proceso.

Mi pregunta es:

Como puedo hacer los pings e interactuar con la interfaz al mismo tiempo?

Espero me puedan ayudar he estado investigando pero no logro solucionarlo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import subprocess
import time
from tkinter import *
 
 
 
def ping(ip):
    command = ["ping", "-n", "1", ip]
    response = subprocess.run(command, timeout=3)
    if response.returncode == 0:
        return True
    else:
        return False
 
def main():
    ip = entry_ip.get()
    print('ENTRY CONTENT: ' + ip)
    if ip != "":
        flag = 0
        while flag <= 4: #Esto deberia hacerce constantemente pero aqui lo remplace por un contador para hacer pruebas
            time.sleep(3)
            result = ping(ip)
            if result == True:
                #flag = True
                flag = flag+1
                label_2.config(bg="white")
                time.sleep(1)
                label_2.config(bg="green")
            else:
                #flag = False
                label_2.config(bg="red")
    else:
        text_1.config(text="ENTER AN IP", font=("Arial", 12))
        label_2.config(bg="red")
 
 
root = Tk()
root.title("IP WD")
#-------------FRAME-------------------
myFrame = Frame()
myFrame.pack()
myFrame.config(bg="gray", relief="groove", bd=10)
#-------------STATIC LABEL 1 TITLE------------
label_1=Label(myFrame, text="IP WATCH DOG\n please enter an IP to verify:\n")
label_1.grid(columnspan=2)
label_1.config(font=("Arial Black", 20))
 
#-------------ENTRY--------------------
entry_ip=Entry(myFrame)
entry_ip.grid(columnspan=3, pady=10)
entry_ip.config(bg="black",fg="green",font=("Arial Black",15), bd=10)
 
#-------------PLAYBUTTON---------------
b_play=Button(myFrame, text="PLAY", command=main)#<<<<<<<<<<<------------------------------------------
b_play.grid(row=2, column=0)
 
#-------------STOPBUTTON---------------
# b_stop=Button(myFrame, text="STOP", command=prueba)
# b_stop.grid(row=2, column=1)
 
#-------------STATIC LABEL 2 VISUAL REFERENCE----------
label_2=Label(myFrame, text="ONLINE\n")
label_2.grid(columnspan=2)
label_2.config(font=("Arial Black", 10), relief="groove", bd=2)
 
#-------------STATIC LABEL 3 BOX MESSAGE----------
text_1=Label(myFrame)
text_1.grid(columnspan=2, pady=10)
text_1.config(width=50, height=10, relief="groove", bd=5, font=("Arial", 12))
 
 
 
 
root.mainloop()
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder