Click here to Skip to main content
15,041,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an error with updating a label in tkinter. I am a school teacher and am on the edge of my ability.

Python
  1  from tkinter import *
  2  global num2
  3  global answer
  4  
  5  
  6  root = Tk()
  7  root.title("hello")
  8  root.geometry("400x400")
  9  
 10  global answerLabel
 11  
 12  def add():
 13      global addmarker
 14      global num1
 15      num1 = int(numEntry.get())
 16      numEntry.delete(first=0,last=4)
 17      addmarker = True
 18      print(addmarker)
 19      
 20      
 21  def answer():
 22      global addmarker
 23      #answerLabel = Label(text = "")
 24      #answerLabel.grid(row = 3, column = 2)
 25      num2 = int(numEntry.get())
 26      print(num1)
 27      print(num2)
 28      print(addmarker)
 29      
 30      if addmarker == True:
 31          answer = num1 + num2
 32          print(answer)
 33      else:
 34          answer = num1 - num2
 35          print(answer)
 36      answerLabel.config(text = answer)
 37      numEntry.delete(first=0,last=4)
 38  
 39      
 40  
 41  def leave():
 42      root.destroy()
 43  
 44  def clear():
 45      numEntry.delete(first=0,last=4)
 46      answerLabel.destroy()
 47      addmarker = False
 48      
 49      
 50  
 51  def subtract():
 52      global num1
 53      global addmarker
 54      addmarker = False
 55      num1 = int(numEntry.get())
 56      numEntry.delete(first=0,last=4)
 57  
 58      
 59  
 60  
 61  numEntry = Entry(root)
 62  numEntry.grid(row = 0, column = 1)
 63  
 64  answerButton = Button(root, text = "Answer", command = answer)
 65  answerButton.grid(row = 1, column = 1)
 66  
 67  addButton = Button(root, text = "+", command = add)
 68  addButton.grid(row = 2, column = 0)
 69  
 70  exitButton = Button(root, text = "Exit", command = leave)
 71  exitButton.grid(row = 2, column = 1)
 72  
 73  clearButton = Button(root, text = "Clear", command = clear)
 74  clearButton.grid(row = 3, column = 1)
 75  
 76  subButton = Button(root, text = "-", command = subtract)
 77  subButton.grid(row = 2, column = 2)
 78  
 79  answerLabel = Label(text = "")
 80  answerLabel.grid(row = 3, column = 2)
 81  
 82  
 83  
 84  root.mainloop()






The error that I get is:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\david\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\david\Downloads\jan14.py", line 36, in answer
answerLabel.config(text = answer)
File "C:\Users\david\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1637, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\david\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1627, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!label"

I feel like crying since I have been staring at a screen for many many hours!!

Any help will be much appriciated!

David

What I have tried:

Everything!! Not sure if why the label will not update when I clear the textbox and then try another addition or subtraction.
Posted
Updated 15-Jan-21 5:53am
v2
Comments
Richard MacCutchan 15-Jan-21 11:13am
   
Sorry about my previous post, it was incorrect. I have downloaded this code and it runs OK.

1 solution

The problem is here:
Python
def clear():
    numEntry.delete(first=0,last=4)
    answerLabel.destroy() # you are destroying the control that you are trying to use elsewhere
    addmarker = False

Change the commented line above to
Python
answerLabel.config(text = "")
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900