简体   繁体   中英

How to write a the content of variable to a text file (python)

I'm trying to figure out how to write the content of a StringVar to a text file in python. Basically I have a dropdown menu and a string variable called value_inside that is the value that the user picks in the dropdown menu. Once the code prints the value that the user picks, I want it to be able to write that value into a text file, but when I try file=open("file.txt", "w") file.write(value_inside) I get an error that says "argument must be str, not StringVar". I think this means that I can't write string variable to a file. Do I need to convert it into something else? Or do I need to put a function that writes the actual value of the string variable into a file? Thank you for your help.

You actually need to invoke the .get() method to get the contents of tk.StringVar()

file=open("file.txt", "w") 
file.write(value_inside)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM