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

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -