0

so i'm trying to make my own login page but i'm running into a error when trying to save the user input into the file. i get a error telling me i cant (.write) a Entry it can only do str. how do i convert or make the entry a str?

from tkinter import *


root = Tk()

#if user clicks submit

def reg():
    file = open("login.txt", "a")
    file.write(login)

#display login
myLabel = Label(root, text="login")
myLabel.pack()

#creates a user entry
login = Entry(root, textvariable="login")
login.pack()
#submit buttton
Button(root, text="submit", command=reg).pack()
root.mainloop()
  • You should remove `textvariable="login"`. `textvariable` is only used when you associate the `Entry` widget with a tkinter variable like `tk.StringVar`. – Henry Yik Apr 24 '20 at 07:11

3 Answers3

0

here I put a example:

from tkinter import *


root = Tk()

#if user clicks submit

def reg():
    value=entry.get() # here you get the string
    print(Value)
    file = open("login.txt", "a")
    file.write(Value)
    file.close()

#display login
myLabel = Label(root, text="login")
myLabel.pack()

#creates a user entry
entry = StringVar()
entryv = Entry(root, textvariable=entry)
entryv.grid(row=1, column=1, columnspan=2,rowspan=1)    
#submit buttton
Button(root, text="submit", command=reg).pack()
root.mainloop()
Sho_Lee
  • 149
  • 4
  • 1
    im kinda new to think but if i understand understand correctly, i tried that. it didnt give me a error but also didnt print – GoToStateFarm Apr 24 '20 at 06:20
0

You can just call the Entry.get() function to get the internal string

with open("file.txt", "w") as f:
    f.write(login.get())
Jimmy Ding
  • 24
  • 3
0

This code works as expected.

from tkinter import *

root = Tk()

#if user clicks submit

def reg(var):
    with open("login.txt", "a") as file:
        file.write(var.get())


#display login
myLabel = Label(root, text="login")
myLabel.pack()

#creates a user entry
login_var = StringVar()
entryv = Entry(root, textvariable=login_var).pack()

#submit buttton
Button(root, text="submit", command= lambda: reg(login_var)).pack()
root.mainloop()
Devansh Soni
  • 771
  • 5
  • 16
  • thanks for the help! i did all that and i got this error " reg() takes 0 positional arguments but 1 was given" – GoToStateFarm Apr 24 '20 at 06:26
  • @GoToStateFarm Probably, you've missed something. Check now, I've put whole code. – Devansh Soni Apr 24 '20 at 06:49
  • do u know how i could do that submitting multiple entrys such as username, password and name? – GoToStateFarm Apr 25 '20 at 05:12
  • Simply pass all of them to the `reg()` function using `command` option in submit button. Something like this: `command = lambda: reg(username, password, name)` – Devansh Soni Apr 25 '20 at 05:31
  • so i tried that and i get a TKinter callback error now... – GoToStateFarm May 16 '20 at 03:18
  • def reg(var): with open("login.txt", "a") as file: file.write(var.get()) #display login myLabel = Label(root, text="Username") myLabel.pack() user_var = StringVar() entryv = Entry(root, textvariable=user_var).pack() myLabel = Label(root, text="Password") myLabel.pack() pass_var = StringVar() entryc = Entry(root, textvariable=pass_var).pack() #submit buttton Button(root, text="submit", command=lambda: reg(user_var, pass_var)).pack() Button(root, text="register").pack() root.mainloop() – GoToStateFarm May 16 '20 at 03:31
  • also. how would i make it delete the input after they press submit? – GoToStateFarm May 16 '20 at 03:35