[Tkinter] showing return from button on main screen - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] showing return from button on main screen (/thread-31046.html) |
showing return from button on main screen - blazejwiecha - Nov-19-2020 Hello, first sorry for English and code, I am beginner :) So , the problem I stuck with is that I wish to execute buttons on the screen of tkinter. For now with lots of problems I got to execute on terminal from PyCharm. Any suggestions will be helpful. import tkinter as tk import random import os import subprocess from subprocess import call, Popen, PIPE TYTUL = "Network helper" PRZYCISKI = [ "Ping Google", "Lista kart sieciowych", "Flush DNS", ] # # Ustawiam Tkinter # okno = tk.Tk() okno.title(TYTUL) # # Funkcje # def czysc_ekran(): # Ta funkcja wyczysci pole tekstowe z calego tekstu ekran.delete('1.0', "end") def aktu_ekran(zawartosc): # Ta funkcja wypisuje "zawartosc" w polu tekstowym ekran.insert("end", zawartosc + "\n") def zbierz_zaznaczone(): # Zbiera te wartosci, które zostaly zaznaczone zebrane = [] for x in dic_przyciski.items(): if x[1].get() == True: zebrane.append(x[0]) return zebrane def ping_google(): # Wypisuje w polu tekstowym ping z Google.com #if ping_google(): czysc_ekran() ekran.insert("end", os.system("ping google.com") + "\n") #aktu_ekran(os.system("ping google.com")) #for x in (os.system("ping google.com")): aktu_ekran(x) #else: # aktu_ekran("Brak zaznaczonych miejsc!") #print('Wyszedłeś na świat za pomocą hosta Google') #os.system("ping google.com") def lista_kart(): # Wypisuje w polu tekstowym wszystkie zaznaczone wartosci if zebrana_lista(): czysc_ekran() aktu_ekran(os.system("ifconfig")) for x in zebrana_lista(): aktu_ekran(x) else: aktu_ekran("Brak zaznaczonych miejsc!") def flush_dns(): # Wypisuje w polu tekstowym wszystkie zaznaczone wartosci if flushed_dns(): czysc_ekran() aktu_ekran("Zaznaczone są:") for x in flushed_dns(): aktu_ekran(x) else: aktu_ekran("Brak zaznaczonych miejsc!") # # Ustawiam Framy # frame_l = tk.Frame(okno) frame_l.pack(side = "left", fill = "both") frame_r = tk.Frame(okno) frame_r.pack(side = "right", fill = "both") # # Ustawiam lewy Frame # Lista checkboxów # W tym Frame zastosuje .pack() do organizacji # Slownik dajacy kazdej miejscowce jej wlasne tk.IntVar() dic_przyciski = {} for x in PRZYCISKI: dic_przyciski[x] = tk.IntVar() # Checkboxy for x in dic_przyciski.items(): tk.Checkbutton(frame_l, text = x[0], variable = x[1], anchor = "w").pack(side = "top", fill = "both") # # Ustawiam prawy Frame # Pole tekstowe i przyciski # W tym Frame zastosuje .grid() do organizacji # Pole tekstowe ekran = tk.Text(frame_r, height = 25, width = 40) ekran.grid(row = 0, column=0, columnspan=5) # Przyciski tk.Button(frame_r, text = PRZYCISKI[0], command = lambda:ping_google()).grid(row = 1, column = 0) tk.Button(frame_r,text = PRZYCISKI[1],command= lambda:lista_kart()).grid(row = 1 , column = 1) tk.Button(frame_r,text = PRZYCISKI[2],command= lambda:flush_dns()).grid(row = 1 , column = 2) # # Tkinter mainloop # okno.mainloop() RE: showing return from button on main screen - Larz60+ - Nov-19-2020 Perhaps this package will help (you won't need to run subprocess): https://pypi.org/project/python-ping/ RE: showing return from button on main screen - blazejwiecha - Nov-20-2020 Your ideas makes me to do something like this: def ping_screen(): ping_command = 'ping -c 5 google.com'.split() proc = subprocess.run(ping_command, capture_output=True, text=True) ekran.insert("end", proc.stdout + "\n") def ping_google(): # Wypisuje w polu tekstowym ping z Google.com czysc_ekran() ping_screen()About the results of ping I got them after it ends pinging . So for now there is -c added to get answer quickly. the problem I stuck for now is how to get it "dynamic" on gui screen ? RE: showing return from button on main screen - deanhystad - Nov-21-2020 subprocess.run() is waits for the subprocess to complete. If you want to update windows while this is happening you'll need to use subprocess.Popen and then write some kind of update event that periodically checks if the subprocess is done and updates the display. RE: showing return from button on main screen - blazejwiecha - Nov-22-2020 Ok got Your ideas, thanks , now I got the code looks like that , but got some issue , when I use another button after few second the ping is showing again on screen. Some hints ? def ping_screen(): #ping_command = 'ping -c 1 google.com'.split() ping_command = 'ping -c 5 google.com' #proc = subprocess.run(ping_command, capture_output=True, text=True) proc1 = subprocess.Popen(ping_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, text=True) print (proc1.pid) time.sleep(5.0) out = proc1.communicate() rc = proc1.wait() # ekran.insert("end", out) #ekran.after(1000, ping_google) #ekran.insert("end", proc.stdout + "\n") def ping_google(): # Wypisuje w polu tekstowym ping z Google.com #if ping_google(): czysc_ekran() #ekran.insert("end", os.system("ping google.com") + "\n") #aktu_ekran(os.system("ping google.com")) #for x in (os.system("ping google.com")): ping_screen() #else: # aktu_ekran("Brak zaznaczonych miejsc!") #print('Wyszedłeś na świat za pomocą hosta Google') #os.system("ping google.com") def kill_google(): # print 'proc1 = ', proc.pid #subprocess.Popen.kill(proc1) proc1.kill() czysc_ekran() |