0

I have been working on with this code and I get my output to the terminal. How do i get the output to print in the tkinter GUI window insted? Here's the code:

import sys import os from tkinter import * def ping(): myinptext = entry.get() os.system("ping "+entry.get()+" -c 2" ) myGui = Tk() entry = StringVar() myGui.geometry('300x300') myGui.title("Get output inside GUI") mylabel = Label(myGui,text="Enter target IP or host as required.").pack() mybutton = Button(myGui,text ="Ping Test",command = ping).pack() myEntry = Entry(myGui,textvariable=entry).pack() myGui.mainloop() 
2
  • read about subprocess and how to get stdout Commented Dec 15, 2016 at 16:24
  • Widget(...).pack() returns None so you assign None to mylabel, mybutton and myEntry. If you really need variable mylabel you have to do in two steps mylabel = Label(..) and mylabel.pack(). But in your example you don't need this variables so you can do without variables - Label(...).pack() Commented Dec 15, 2016 at 16:28

1 Answer 1

1

Use subprocess instead of os.system. There are many functions to work with external command.

I use subprocess.check_output() to get result of executed command. Command has to be as list ["ping", entry.get(), "-c", "2"]. Command can be single string if you use shell=True.

import tkinter as tk import subprocess def ping(): cmd = ["ping", entry.get(), "-c", "2"] output = subprocess.check_output(cmd) #output = subprocess.check_output("ping {} -c 2".format(entry.get()), shell=True) print('>', output) # put result in label result['text'] = output.decode('utf-8') my_gui = tk.Tk() entry = tk.StringVar() my_gui.geometry('300x300') my_gui.title("Get output inside GUI") tk.Label(my_gui, text="Enter target IP or host as required.").pack() tk.Entry(my_gui, textvariable=entry).pack() tk.Button(my_gui,text="Ping Test", command=ping).pack() # label for ping result result = tk.Label(my_gui) result.pack() my_gui.mainloop() 

BTW: because ping takes some time so tkinter will freeze for this time. If you need non-freezing version you will need other functions in subprocess or threading module.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks @furas that worked just fine. Much appreciated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.