【问题标题】:how to save text into a txt file using python 2.7 using tk如何使用 tk 使用 python 2.7 将文本保存到 txt 文件中
【发布时间】:2013-05-06 01:29:19
【问题描述】:

我正在使用 tkinter,我正在尝试将用户输入的文本保存到现有的 txt 文件中,当他们点击保存时,任何想法。

from Tkinter import *

root = Tk()

w1 = Label(root, text="Username")
w1.pack()

e = Entry(root)
e.pack()

w2 = Label(root, text="Password")
w2.pack()

e1 = Entry(root)
e1.pack()

toolbar = Frame(root)

b = Button(toolbar, text="save", width=9)
b.pack(side=LEFT, padx=2, pady=2)

toolbar.pack(side=TOP, fill=X)

mainloop()

【问题讨论】:

    标签: python button tkinter save


    【解决方案1】:

    最简单的方法是创建一个在单击保存按钮时调用的函数。将其放在脚本顶部附近,然后将其设置为按钮的command

    def save():
        text = e.get() + " " + e1.get() + "\n"
        with open("text.txt", "a") as f:
            f.write(text)
    
    # Snip
    
    b = Button(toolbar, text="save", width=9, command=save)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多