Python Forum
Help with Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Tkinter
#1
from tkinter import *

total = 0
num = 0

def click():
    number = num_box1.get() # defines number variable as number entered into box1
    number = int(number) # defines it as an integer
    answer = num_box2["text"] # sets the variable answer as the contents of num_box2
    answer = int(answer) # defines as an integer
    num_box1["bg"] = "white" # sets box background as white
    num_box1["fg"] = "black" # sets box font to black
    total  = number + answer # adds new number to the total 
    num_box2["text"] = answer # sets the total as the text in box2 

def reset():
    total = 0 # resets the total to 0
    num_box1["text"] = "" # empties num_box1
    num_box1.delete(0,END) # deletes the contents of box1
    num_box2["text"] = 0 # resests box2 to 0
    num_box1.focus() # returns cursor to box1

window = Tk() # creates tkinter window 
window.geometry("1000x500") # with this geometry

num_box1 = Entry(text = "")
num_box1.place(x = 150, y = 50, width = 200, height = 25) # box position and size
num_box1["bg"] = "white" # box background is set to white
num_box1["fg"] = "black" # box font is set to black

num_box2 = Message(text = total) # creates a box which displays the total
num_box2.place(x = 225, y = 100, width = 200, height = 30) # defines boxes place and dimensions
num_box2["bg"] = "yellow" # sets boxes background to yellow
num_box2["fg"] = "blue" # sets boxes font to blue

button1 = Button(text = "Add to the total", command = click) # creates a button which runs click() when pressed
button1.place(x = 30, y = 50, width = 120, height = 25) # defines button place and dimensions

button2 = Button(text = "Reset the total to zero", command = reset) # creates a button which runs the reset program
button2.place(x = 60, y = 100, width = 120, height = 25) # defines button place and dimensions

window.mainloop() # allows program to keep running
Hi the above code gives me the following error

Error:
File "C:\Users\djwil\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "c:/Users/djwil/Documents/python/learning python/Chapter 16 - Tkinter GUI/Ch16-c3.py", line 6, in click AttributeError: 'Message' object has no attribute 'get'
I've put the box where I enter the variable as Entry in the code, so I don't understand why it gives me this message? Can anyone explain why I'm getting this error message?

Thanks
Reply
#2
I don't have this error. The code that you posted doesn't seem to correspond to the error message.
At line 14 it should be num_box2["text"] = total instead of num_box2["text"] = answer
Reply
#3
Thanks :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020