Help with Tkinter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Help with Tkinter (/thread-28767.html) |
Help with Tkinter - djwilson0495 - Aug-02-2020 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 runningHi the above code gives me the following error 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 RE: Help with Tkinter - Gribouillis - Aug-02-2020 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
RE: Help with Tkinter - djwilson0495 - Aug-03-2020 Thanks :) |