Lenton
Lenton

Reputation: 519

Take text from textbox and use it in Python 2.7

I'm pretty new in programming, so I don't know many basics. I tried searching everywhere, but didn't get the answer I need.
In this website I found a similar problem, but it was for Python3.
I could change to python 3 interpreter, but then I would have to rewrite the code, due to syntax.

Anyway, my problem is, I want to write down text in a textbox and I need it to be taken for use (for example print it out or use it as name in a linux command).
I tried raw_input, even tried to add .get commands.
.get didn't work for me and input or raw_input input would do nothing, they would not print out the text and my programming gets stuck

My code

def filtras():
    root = Tk()
    root.title("Filtravimas pagal uzklausa")
    root.geometry("300x100")
    tekstas = Text(root, height=1, width=15).pack(side=TOP)
    virsus = Frame(root)
    virsus.pack()
    apacia = Frame(root)
    apacia.pack(side=BOTTOM)
    myg1 = Button(virsus, text="Filtruoti", command=lambda: gauti())
    myg1.pack(side=BOTTOM)

def gauti():
    imti=input(tekstas)
    print(imti)

Upvotes: 0

Views: 1024

Answers (1)

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19144

Your problem is a common mistake in this line:

tekstas = Text(root, height=1, width=15).pack(side=TOP)

Geometry methods such as .pack and .grid return None, and None.get does not work. Instead do the same as you did for Frame and Button.

tekstas = Text(root, height=1, width=15)
tekstas.pack(side=TOP)

Now tekstas.get() and other Text methods will work.

This code does not touch on 2 <=> 3 syntax changes. The only issue is the name Tkinter versus tkinter and other module name changes.

Please read about MCVEs. Everything after the Text widget is just noise with respect to your question.

input and raw_input get characters from stdin, which is normally the terminal. Except for development and debugging, do not use them with GUI programs, and only use them if running the GUI program from a terminal or IDLE or other IDE. Ditto for print.

Upvotes: 1

Related Questions