简体   繁体   English

如何将变量的内容写入文本文件(python)

[英]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.我想弄清楚如何将 StringVar 的内容写入 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.基本上我有一个下拉菜单和一个名为 value_inside 的字符串变量,它是用户在下拉菜单中选择的值。 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".一旦代码打印了用户选择的值,我希望它能够将该值写入文本文件,但是当我尝试file=open("file.txt", "w") file.write(value_inside)我收到一条错误消息,指出“参数必须是 str,而不是 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()您实际上需要调用.get()方法来获取tk.StringVar()的内容

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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