【问题标题】:Saving input from entry box into .txt files将输入框的输入保存到 .txt 文件中
【发布时间】:2018-07-02 10:46:23
【问题描述】:

我想将输入框的输入保存到.txt 文件中,它适用于第一个代码而不是第二个代码,但我需要使用的是第二个代码。

代码 1:

import tkinter as tk

def f():
    def save():
        a = t.get()
        f = open((a + '.txt'), 'w')
        f.write(a)
        f.close()
        return

   top = tk.Tk()
   t = tk.StringVar()
   e = tk.Entry(top, textvariable = t).pack()
   b = tk.Button(top, text = 'Save as a file', command = save).pack()
   top.mainloop()
f()

代码 2:

import tkinter as tk

root = tk.Tk()

def f():
    def save():
        a = t.get()
        f = open((a + '.txt'), 'w')
        f.write(a)
        f.close()
        return

    top = tk.Tk()
    t = tk.StringVar()
    e = tk.Entry(top, textvariable = t).pack()
    b = tk.Button(top, text = 'Save as a file', command = save).pack()
    top.mainloop()

button = tk.Button(root, text="Button",command=f).pack()
root.mainloop()

【问题讨论】:

标签: python tkinter file-handling


【解决方案1】:

您将变量与输入框混淆了:使用更好的变量名称会有所帮助。您还在使用此名称创建的文件中写入file_name...目前尚不清楚它是否真的是您想要的。 您还与分配给变量 e 的同一行打包 - pack() 返回 None
出于某种原因,您还启动了两个主循环;不要这样做,这是个坏主意。

import tkinter as tk

def save():
    file_name = entry.get()
    with open(file_name + '.txt', 'w') as file_object:
        file_object.write(file_name)   # it is unclear if writing the file_name in the newly created file is really what you want.

if __name__ == '__main__':
    top = tk.Tk()
    entry_field_variable = tk.StringVar()
    entry = tk.Entry(top, textvariable=entry_field_variable)
    entry.pack()
    tk.Button(top, text="save", command=save).pack()

    top.mainloop()

我删除了嵌套函数;如果你觉得有必要这样做,也许你应该改用一个类。
我还将文件的打开/关闭更改为为您处理它的上下文管理器。

【讨论】:

    【解决方案2】:
    import tkinter as tk
    
    def f():
        def save():
            a = t.get()
            with open((a + '.txt'), 'w') as f: 
                f.write(a)
    
        top = tk.Tk()
        t = tk.StringVar(top)
        tk.Entry(top, textvariable=t).pack()
        tk.Button(top, text = 'Save as a file', command=save).pack()
    
    root = tk.Tk()
    tk.Button(root, text="Button", command=f).pack()
    root.mainloop()
    

    最重要的变化是t = tk.StringVar() -> t = tk.StringVar(top),指定了主小部件。还有一些其他更改(例如pack() 返回None,所以不要基于它设置值,使用上下文管理器来关闭文件)

    【讨论】:

    • 你不能有两个 Tk 实例。
    • @Lafexlos 当然可以,您可能不应该,因为可能有更好的方法,但代码显然有效。 (试试看)
    • @Lafexlos 您是否阅读了 you 链接的建议副本? “从技术角度来看,没有理由不能同时拥有两个 Tk 实例。”
    • 嗯,是的,can't 是一个很强烈的词,我对此不好。
    • @jkle,如果您接受我的回答是因为它比其他人更接近您的回答,那主要是因为我对它的考虑比其他人少。您应该考虑将 ReblochonMasque 的答案标记为正确。即使您决定使用这种方法,其他读者也可能对更通用的解决方案感兴趣。
    【解决方案3】:

    您没有在第二个代码中调用 save() 函数。在定义后添加save() 调用将解决您的问题。

    您的代码:

    def f():
        def save():
            a = t.get()
            f = open((a + '.txt'), 'w')
            f.write(a)
            f.close()
            return
    

    固定:

    def f():
        def save():
            a = t.get()
            f = open((a + '.txt'), 'w')
            f.write(a)
            f.close()
            return
        save()
    

    【讨论】:

    • 见:tk.Button(top, text = 'Save as a file', command = save).pack()
    猜你喜欢
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多