简体   繁体   English

如何从tkinter获取条目并将其打印在txt文档中?

[英]How can I take an entry from tkinter and print it in a txt document?

I want what I get from function Sighnup and put it in 'Users.txt' and any tips in shortening TK code? 我想要从Sighnup函数中获取的内容并将其放在“ Users.txt”中,以及有关缩短TK代码的任何提示吗? I'm new to TK so any tips or tricks wpould be nice too!:) Oh and this is a program for an app my friend is making so I need it to work very well This is ~1/2 the code so i might need more help. 我是TK的新手,所以任何技巧也都不错!:)哦,这是我朋友正在制作的应用程序程序,所以我需要它能很好地工作这是〜1/2代码,所以我可能需要更多帮助。 Don't worry this is only one of the windows I have a login window too. 不用担心,这只是我也有登录窗口的窗口之一。 I know there is two Sighnup functions but it works well so i'm keeping it that way.When I do the (ent.get()) it prints it on the shell not the txt but It made the txt file but won't write in it. 我知道有两个Sighnup函数,但是它工作得很好,所以我一直保持这种方式。当我执行(ent.get())时,它将它打印在shell上而不是txt上,但它制作了txt文件,但不会写在里面。

import tkinter
def Sighnup():
    window2 = tkinter. Tk()
    def Quit2 ():
        window2.destroy()
    def Sighnup():
        open ('Users.txt','w')
        (ent.get())
        (ent2.get())
        (ent3.get())
        (ent4.get())
        (ent5.get())
        window2.destroy()
    window2.geometry("195x135")
    window2.title("Sighnup")
    window2.wm_iconbitmap('favicon.ico')
    lbl= tkinter.Label(window2, text="First Name:")
    lbl2= tkinter.Label(window2, text="Last Name:")
    lbl3= tkinter.Label(window2, text="Email:")
    lbl4=  tkinter.Label(window2, text="Username:")
    lbl5= tkinter.Label(window2, text="Password:")
    ent= tkinter.Entry(window2)
    ent2= tkinter.Entry(window2)
    ent3= tkinter.Entry(window2)
    ent4= tkinter.Entry(window2)
    ent5= tkinter.Entry(window2)
    btn= tkinter.Button(window2, text="Submit", command=Sighnup)
    btn2= tkinter.Button(window2, text="Quit", command=Quit2)
    lbl.grid(row=0, column=0)
    ent.grid(row=0, column=1)
    lbl2.grid(row=1, column=0)
    ent2.grid(row=1, column=1)
    lbl3.grid(row=2, column=0)
    ent3.grid(row=2, column=1)
    lbl4.grid(row=3, column=0)
    ent4.grid(row=3, column=1)
    lbl5.grid(row=4, column=0)
    ent5.grid(row=4, column=1)
    btn2.grid(row=5, column=1)
    btn.grid(row=5, column=0)
    window2.mainloop()

Just opening a file doesn't make output go there, you need to write to it: 仅打开文件并不能使输出到达那里,您需要写入该文件:

fout = open('Users.txt', 'w')
fout.write(ent.get())
...

Or better, use a context manager 或者更好,使用上下文管理器

with open('Users.txt', 'w') as fout:
    fout.write(ent.get())

As far as suggestions to clean things up, I would use loops to create the widgets and lists to store them. 至于清理的建议,我将使用循环来创建小部件和列表来存储它们。

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

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