Search code examples
pythonstringtkintertkinter-entry

Writing string variables from Tkinter.Entry


I am using Python 2.5 (for Autodock). I have made a simple GUI to take values from the user, I am trying to generate an output file using the said values but I keep getting errors like: "TypeError: argument 1 must be string or read-only character buffer, not instance" Here is some of my code:

import sys
from Tkinter import * #for GUI
from tkFileDialog import * #for browse button function

mGui = Tk()

mGui.geometry('500x400+300+100')#dimenstions and position from top left
mGui.title('Autodock compiler')#window title

...

def generate():
    conf = open('invoke(browsebutton3)''mEntry10'".txt","w")
...
    conf.write("center_x =")
    conf.write(e1)
    conf.write("\n")
...
    conf.close()
    return
...
e1=StringVar()
mlabel3 = Label(text='Center x')
mlabel3.place (x=30,y=140)
mEntry1 = Entry(mGui,textvariable=e1)
mEntry1.place(x=100,y=140)

I have included the relevant code.


Solution

  • You have to get the StringVar's value by invoking the get method:

    conf.write(e1.get())
    

    Below is a very simple script to demonstrate:

    from Tkinter import StringVar, Tk
    Tk()
    s = StringVar()
    s.set('word')
    print s, type(s)
    print s.get(), type(s.get())
    

    Output in terminal:

    PY_VAR0 <type 'instance'>
    word <type 'str'>