【问题标题】:I want to save an input from my GUI in a textfile, using TKinter. how can I save to text file from entries我想使用 TKinter 将我的 GUI 中的输入保存在文本文件中。如何从条目保存到文本文件
【发布时间】:2017-09-22 13:54:03
【问题描述】:
def submit(self):
    if get1 =="":
        print('please input a name')
    else:
        with open('users.txt',"a") as f:
            f.write(get1)
        f.close() 

users.txt 是我的文件,如下所示,namee 是 Entry 变量, 全部使用 tkinter

self.namee = Entry(frame)
self.namee.grid(row=7,column=1)

这是我做的条目

get1 = self.namee.get()

这是我做的吸气剂:

self.submit = Button(frame, text="Submit",command=self.submit)
self.submit.grid(row = 26, column=0, sticky=W)

这是运行函数的按钮,开始 if 语句

【问题讨论】:

  • 您已经告诉我们您想要什么,但没有告诉我们您需要什么帮助。你的问题没有问题。您是否学习过 tkinter 教程,是否阅读过 Entry 和其他小部件的文档?
  • 这个问题的答案应该很有用stackoverflow.com/questions/9815063/…
  • 我现在已经使用提供的链接再次编辑了代码,当我单击提交按钮时,显然条目是空的,因为我的“users.txt”文件中没有任何内容

标签: python-3.x user-interface tkinter text-files


【解决方案1】:

您没有在提交函数中获得输入字段的值。试试这个

def submit(self):
    get1 = self.namee.get()
    if get1 =="":
        print('please input a name')
    else:
        with open('users.txt',"a") as f:
            f.write(get1)
        #f.close() # Not needed, with closes f for you.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-24
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 2011-02-01
    • 1970-01-01
    相关资源
    最近更新 更多