I am trying to put a terminal into my tk gui program as a widget. I figured out how to have the output of a command sent to a text widget:

root = Tkinter.Tk()
tfield = Tkinter.Text(root)
tfield.pack()
for line in os.popen("run_command", 'r'):
	tfield.insert("end", line)

root.mainloop()

But how can I send info to it? Also, I want to be able to select information from the terminal output as an argument for a function. For example the click of a button launches the command 'iwconfig'. This prints five different wireless extensions and their info into a text widget. Now I want the user to select which extension we will be working with by clicking that (such as wlan0) in the text box. I will later launch another command into the text box which will ask the user to confirm an action by typing 'y'. I would like to do this automatically.

I know this sounds like I'm just asking instead of trying myself, but I can't find anything on this for python. I did find one post of virtually the same question, but it was for perl, and I am 'uni-lingual'.(lol)

Initialize your widget attribute textvariable with StringVar(). Use w.set() to set the value and w.get() to get the value.

I have the general idea, but still can't seem to capture the input. I want my variable (x) to contain an entire line from the tfield, but I don't think I'm using linestart and lineend properly. Here's what I have:

import Tkinter
import os

def get_info(arg):
	x = Tkinter.StringVar()
	x = tfield.get("linestart", "lineend") # gives an error 'bad text index "linestart"'
	print x
	
root = Tkinter.Tk()
tfield = Tkinter.Text(root)
tfield.pack()
for line in os.popen("iwconfig", 'r'):
	tfield.insert("end", line)
tfield.bind("<Return>", get_info)

root.mainloop()

You don't need to initialize a StringVar() using a Tkinter.Text widget. Here's an example using "ping":

import Tkinter
import os

ip = '192.168.1.1'

def get_info(arg):
    print tfield.get("1.0", "current lineend")

root = Tkinter.Tk()
tfield = Tkinter.Text(root)
tfield.pack()
f = os.popen('ping %s' % (ip))
for line in f:
    line = line.strip()
    if line:
        tfield.insert("end", line+"\n")
        # tfield.get("current linestart", "current lineend")
tfield.bind("<Return>", get_info)

root.mainloop()
f.close()

In the example, get_info() retrieves all the text in the Text window.

This question has already been answered. Start a new discussion instead.