简体   繁体   中英

How do I get the text from an entry box in python?

I tried creating a login system. it gives me a: 'NoneType' object has no attribute 'get'

here is the code:

import tkinter
from tkinter import ttk

window = tkinter.Tk()

def addUsernm():
    a = usernl.get("1.0", 'end-1c')
    print(a)

usernames = []
passwords = []

usernl = tkinter.Entry(window).pack()
passwrdnl = tkinter.Entry(window).pack()



submitbtn = tkinter.Button(window,text="submit",command=addUsernm).pack()

window.mainloop()

How do I fix this?

The issue is, you're confusing the Text widget get() method, with the Entry widget get() method, and they aren't the same method. Also you can't use pack() on the same line as widget or the variable won't be saving anything. This is the solution that worked for me:

 from tkinter import * window = Tk() window.state("zoomed") def add(): a = user.get() b = password.get() print(a,b) user = Entry(window) user.pack() password = Entry(window) password.pack() submitbtn = Button(window,text="submit",command=add).pack() window.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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