python 3.x - Code not working trying to write the content of a StringVar to a file -


i've got lot of experience basic programming, none gui programming, , teaching myself python 3 tkinter purpose. i've got great answers checking stackoverflow posts, i've got problem doesn't seem have been covered. want program user input entry widget , write data file. after stripping out nonessential lines, current code is

from tkinter import * tkinter import ttk  root = tk()  sheetid = stringvar()  def finish():     open('c:/python33/data.txt', 'w') f:         f.write('first line of text\n')     open('c:/python33/data.txt', 'a') f:         f.write(sheetid)  main = frame(root).grid() ttk.entry(main, textvariable="sheetid").grid(row=0, column=1) ttk.button(main, text="close", command=finish).grid(column=1)  root.mainloop() 

i've used label widget write variable "sheetid"in window, know combination of entry widget , "close" button widget working. first write statement works, know file-opening code correct. second write gives run-time error:

typeerror: must str, not stringvar.

next tried converting "sheetid" string, using

s = str(sheetid,'\n') 

but returned run-time error:

typeerror: coercing str: need bytes, bytearray or buffer-like object, stringvar found.

surely there way this, google search hasn't found it.

thanks!

you cannot use ..., textvariable="sheetid", must use ..., textvariable=sheetid, ... (notice lack of quotes around sheetid). , then, when want write value out must use sheetid.get().


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -