-1
import time 
import tkinter
from tkinter import *
import tkinter as tk
 
stonestotal = 0
def metricimperial(Amount):
    Amount = stonestotal.get()
    global gramstotal
    gramstotal = Amount * 0.00220462
    print(gramstotal)
    final()
    
def choice():
    conversion = input("metric or imperial conversion ")
    
def imperialmetric(Amount):
    Amount = stonestotal.get()
    global gramstotal
    gramstotal = Amount * 453.592
    print(gramstotal)
    final()
 
window = Tk()
 
Labelframe = LabelFrame(window, text="this is a LabelFrame").grid()
stonestotal = tkinter.IntVar(value=1)
inputbox = Spinbox(Labelframe, textvariable=stonestotal, from_=0, to = 99999999999999999).grid(row = 0, column = 0)
Amount = stonestotal.get()
imperial = Button(Labelframe,text="imperial to metric",command = lambda:imperialmetric(Amount)).grid(row=1,column=0)
metric = Button(Labelframe,text="metric to imperial",command = lambda:metricimperial(Amount)).grid(row=1,column=1)
def final():
    label = Label(Labelframe, text = gramstotal).grid()
    
    label.after(5, label.master.destroy)
    
 
 
window.mainloop()

when I run the code it works for the most part outputting the converted value however it does not remove the current value and instead places the next output underneath stacking indefinitely whilst giving the error I get a error stating:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:/Python39/yr11 code.py", line 31, in <lambda>
    metric = Button(Labelframe,text="metric to imperial",command = lambda:metricimperial(Amount)).grid(row=1,column=1)
  File "C:/Python39/yr11 code.py", line 12, in metricimperial
    final()
  File "C:/Python39/yr11 code.py", line 35, in final
    label.after(5, label.master.destroy)
AttributeError: 'NoneType' object has no attribute 'after'

I don't know what's causing this any help would be much appreciated

plr108
  • 1,201
  • 11
  • 16
benjamin
  • 9
  • 1
  • and again the common mistake of using `Widget.layout()` which returns `None`. split it in two lines: `label = Label(*args, **kwargs)` and `label.layout_manager()` (where `layout_manager` is either pack or grid or place) – Matiiss Jul 12 '21 at 11:50

1 Answers1

1

All geometry managers: grid(),pack(),place() return None, or don't return anything. Ultimately, the variable label is assigned None:

Move .grid() to next line

label = Label(Labelframe, text = gramstotal)
label.grid()