-2

my code is very simple, but I receive this error

Traceback (most recent call last): File "F:/Download/PY/Timer2.py", line 10, in e1.insert(0,"5") AttributeError: 'NoneType' object has no attribute 'insert'

import tkinter
from tkinter import *

root = tkinter.Tk()
root.title(string='prova')
root.configure(background='lightgray')
lbl_title = Label(root, padx=10, text="Timer", fg='black', bg='lightgray', font='Times 24', anchor='center').grid(row=0)
lbl_time = Label(root, font="Times 38", fg='black', bg='lightgray', width=8).grid(row=1)
e1 = Entry(root,font="Times 22", fg='black', bg='white', width=6).grid(row=2, column=0)
e1.insert(0,"5")
btn_start = Button(root, text='START', bg='black', fg='white', font='Times 24').grid(row=2, column=1)
root.mainloop()
patel
  • 430
  • 1
  • 4
  • 9

1 Answers1

1

If you try to print the value of e1, you will realize that it is actually None. That's because you have used the grid() method just after you have defined the Entry widget and grid() returns None. So, you need to separate them. Here is the working code.

from tkinter import *

root = Tk()
root.title(string='prova')
root.configure(background='lightgray')
lbl_title = Label(root, padx=10, text="Timer", fg='black', bg='lightgray', font='Times 24', anchor='center').grid(row=0)
lbl_time = Label(root, font="Times 38", fg='black', bg='lightgray', width=8).grid(row=1)
e1 = Entry(root,font="Times 22", fg='black', bg='white', width=6) ##
e1.grid(row=2, column=0) ##
e1.insert(0,"5")
btn_start = Button(root, text='START', bg='black', fg='white', font='Times 24').grid(row=2, column=1)
root.mainloop()

enter image description here

Also, now you should realize that your other variables are also None. Here is the new correct code.

import tkinter as tk

root = tk.Tk()
root.title(string='prova')
root.configure(background='lightgray')
tk.Label(root, padx=10, text="Timer", fg='black', bg='lightgray', font='Times 24', anchor='center').grid(row=0)
tk.Label(root, font="Times 38", fg='black', bg='lightgray', width=8).grid(row=1)
e1 = tk.Entry(root,font="Times 22", fg='black', bg='white', width=6)
e1.grid(row=2, column=0)
e1.insert(0,"5")
tk.Button(root, text='START', bg='black', fg='white', font='Times 24').grid(row=2, column=1)
root.mainloop()
Miraj50
  • 4,257
  • 1
  • 21
  • 34