admin管理员组

文章数量:1122832

I've got a method of a class that is used to register the user, and has some tkinter entry with:

email=tk.StringVar()
nameent = tk.Entry(registerwindow,textvariable = email, font=('Arial',14,'normal'))

but when I try to get the value that has been inputted in the name entry it returns a blank result

I've tried getting it to output the entry value at multiple stages in the user registration process to see where it was erroring but I found that it doesn't even seem that it picks up the value from the entry from the beginning

I've got a method of a class that is used to register the user, and has some tkinter entry with:

email=tk.StringVar()
nameent = tk.Entry(registerwindow,textvariable = email, font=('Arial',14,'normal'))

but when I try to get the value that has been inputted in the name entry it returns a blank result

I've tried getting it to output the entry value at multiple stages in the user registration process to see where it was erroring but I found that it doesn't even seem that it picks up the value from the entry from the beginning

Share Improve this question edited Dec 21, 2024 at 17:25 toyota Supra 4,5338 gold badges21 silver badges24 bronze badges asked Nov 21, 2024 at 11:31 Charlotte WatsonCharlotte Watson 1 3
  • 1 Most probably you have created more than one instance of Tk(). – acw1668 Commented Nov 21, 2024 at 11:53
  • Please edit your question to provide a complete minimal reproducible example, otherwise it's difficult for us to diagnose what might be happening. – JRiggles Commented Nov 21, 2024 at 15:36
  • It could be that email is a local variable that is getting destroyed by the garbage collector. Without a proper minimal reproducible example it's impossible for us to say for sure. – Bryan Oakley Commented Nov 21, 2024 at 23:15
Add a comment  | 

1 Answer 1

Reset to default 0

I assume that you want to use the user entered email. For instance, you may want to validate the email with a function or check whether email exists in the database. In that case, you don't need to declare a StringVar. By using nameent.get(), you'll get the user entered email which you can use it in a function. For the sake of simplicity I've given my example without class which you can convert into class and function.

import tkinter as tk

def get_email():
    email = nameent.get()
    print(email)
    
root = tk.Tk()
root.geometry("300x200")

registerwindow= tk.Frame(root)
registerwindow.pack()

nameent = tk.Entry(registerwindow,font=('Arial',14,'normal'))
nameent.pack()

btn = tk.Button(registerwindow, text = "Get Email", command = get_email)
btn.pack()

root.mainloop()

本文标签: pythontkinter entry windows not working properly in oopStack Overflow