Home>

I am making a calorie consumption calculation app with tkinter.
I got the following error message and the result was the same even if I renamed the file.
I don't understand why only line 37 causes an error due to insert.
If i can understand it, thank you.

PS C: \ Users \ Elysium \ .vscode>&C:/Users/Elysium/AppData/Local/Programs/Python/Python38-32/python.exe c: /Users/Elysium/.vscode/m_enr.py
Traceback (most recent call last):
File "c: /Users/Elysium/.vscode/m_enr.py", line 37, in
texthour = tk.insert (tk.END, '3')
AttributeError: module'tkinter' has no attribute'insert'
PS C: \ Users \ Elysium \ .vscode>Enter the language here code #as You can give the library any abbreviation you like
import tkinter as tk

Processing when the button is pressed --- (* 1) def function definition () A device for inserting arguments. Not defined here

def calc_enr ():

Calculate calories burned float A type that handles floating point numbers. Used when I want to tolerate calculation errors get () Method for retrieving data by specifying the key Method: A collection of multiple processes

m = float (textmets.get ())
h = float (textHour.get ())
w = float (textWeight.get ())
enr = m * h * w

Display the result on the label {} .format replacement field .format (), {} contains the argument I want to replace

s ='calories burned {} kcal'.format (enr)
labelResult ['text'] = s

Create window --- (* 2)

win = tk.Tk ()
win.title ("Climbing energy consumption")

Specify window size

win.geometry ("500x250")

Create parts --- (* 3)

labelmets = tk.Label (win, text = u'Mets:')
labelmets.pack ()

textmets = tk.Entry (win)
textmets.insert (tk.END, '7.3')
textmets.pack ()

labelhour = tk.Label (win, text = u'action time:')
labelhour.pack ()

texthour = tk.Entry (win)
texthour = tk.insert (tk.END, '3')
texthour.pack ()

labelWeight = tk.Label (win, text = u'Weight including luggage (kg):')
labelWeight.pack ()

textWeight = tk.Entry (win)
textWeight = tk.insert (tk.END, '60')

labelResult = tk.Label (win, text = u'

  • Answer # 1

    Why insert around line 29? Works fine
    I think it's obvious why the insert on line 37 doesn't work properly.

    # Syntax around line 29
    textmets.insert (tk.END, '7.3')
    # Syntax on line 37 where the error is occurring this time
    texthour = tk.insert (tk.END, '3')

    If you use insert to insert a string into the entry widget
    You need to specify the target widget.

    Specified widget = tk.Entry (win)
    Widget .insert (tk.END,'text you want to insert')
    Specified widget .pack ()