Python Forum
[Tkinter] showing return from button on main screen
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] showing return from button on main screen
#1
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()
Reply
#2
Perhaps this package will help (you won't need to run subprocess):
https://pypi.org/project/python-ping/
Reply
#3
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 ?
Reply
#4
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.
Reply
#5
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter showing image in button rwahdan 3 5,528 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  [Kivy] Kivy pop up shows duplicate buttons from main screen CEKinch 0 1,922 Feb-05-2021, 05:40 PM
Last Post: CEKinch
  [PyQt] QPainter issue showing black screen mart79 0 1,952 May-06-2020, 12:02 PM
Last Post: mart79
  [Tkinter] Listbox and Button not showing Linuxdesire 2 3,793 Oct-05-2019, 06:33 PM
Last Post: woooee
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,951 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  “main thread is not in main loop” in Tkinter Long_r 1 24,092 Jun-26-2019, 11:00 PM
Last Post: metulburr
  [Tkinter] RE: status bar to return to the centre after 1 minute of clicking a button ? chano 6 4,615 May-27-2019, 04:24 PM
Last Post: Yoriz
  Unable to return value from callback function of a button in Python Tkinter nilaybnrj 4 20,702 Aug-05-2018, 11:01 PM
Last Post: woooee
  Tkinter touch screen button click ashtona 2 9,581 Apr-13-2018, 11:46 AM
Last Post: buran
  GTK main window calling a main window DennisT 4 6,697 Oct-19-2016, 09:36 PM
Last Post: DennisT

Forum Jump:

User Panel Messages

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