You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Tkinter登录GUI代码中的AttributeError: 'NoneType' object has no attribute 'get'

Fixing AttributeError: 'NoneType' object has no attribute 'get' in Tkinter Login App

Hey there! That error you're hitting is a super common Tkinter gotcha—let's break down what's going wrong and fix it right away.

The Root Cause

In your code, you're creating widgets and calling .grid() all in one line, like this:

entry = Entry(frame, bd = 5 , font = "Lucida 30 bold" , width = 25).grid(row = 1 , column = 1)

Here's the problem: Tkinter's .grid() method returns None, not the widget itself. That means your entry and serialentry variables are actually storing None instead of the Entry widget instances. When you try to call .get() on them in Submitcommand(), you're trying to run a method on None—hence the AttributeError.

The Simple Fix

You need to split widget creation and layout into separate lines. First create the widget and assign it to a variable, then call .grid() on that variable. Here's your corrected code:

from tkinter import *
root1 = Tk()
root1.geometry("700x330+350+300")
key = "Agcy5x7kZe"
frame = LabelFrame(root1)

def Submitcommand():
    a = entry.get()
    b = serialentry.get()
    print(a)# To check
    print(b)# To check

photo = PhotoImage(file = 'gil.png')
photo1 = Label(frame , image = photo)
photo1.grid(row = 0 , column = 0 , columnspan = 2 , pady = (0 , 20))

username = Label(frame, text = "Username :" , font = "helvetica 30 bold")
username.grid(row = 1 , column = 0)

entry = Entry(frame, bd = 5 , font = "Lucida 30 bold" , width = 25)
entry.grid(row = 1 , column = 1)

label = Label(frame, text = "Serial Key : " , font = "helvetica 30 bold")
label.grid(row = 2 , column = 0)

serialentry = Entry(frame, bd = 5 , font = "Lucida 30 bold" , width = 25)
serialentry.grid(row = 2 , column = 1)

submit = Button(frame , text = "Login" , height = 2 , width = 10 , command = Submitcommand)
submit.grid(row = 3 , column = 1 , columnspan = 2 , pady = (0 , 20))

frame.place(relx = 0.5 , rely = 0.5 , anchor = CENTER)
root1.mainloop()

What Changed

  • For every widget (Label, Entry, Button), we first create the widget and store it in a variable (e.g., entry = Entry(...)), then call .grid() on that variable (e.g., entry.grid(...)). This ensures your variables hold actual references to the widgets, not None.
  • All your original functionality stays intact—this just fixes the variable assignment issue that was causing the error.

Now when you click the Login button, entry.get() and serialentry.get() will correctly pull the text from your input fields without throwing an error.

内容的提问来源于stack exchange,提问作者Anush Kamble

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 15:07:59