I am facing this error which shows in the screenshot, when I move my bind function into a class or another module
showing as entry is not defined.. it worked fine without a class
import tkinter
from tkinter import *
root = tkinter.Tk()
root.wm_title("speed entering calculator")
class Methods1:
i=0
#function for enter
def entering(self,a):
global i #to inform this function that i is global variable
try: # try and exception used to print message if error happen
value1=int (entry1.get()) #entry1.get()is used to get values which entered in entry box
value1=i + value1
label1['text']=value1
i=value1
except ValueError as ve:
print(f'only integer')
entry1.delete(0, END) # used to clear entry box
ob=Methods1()
#creating canvas
canvas1 = tkinter.Canvas(root, height=400 , width=400, bg="black")
canvas1.pack()
#making entry box for values
entry1 = tkinter.Entry(root, justify="center", )
canvas1.create_window(200,140,height=100,width=100, window=entry1)
value = "the values should be shown here"
label1=Label(root, text =value, height=10)
label1.pack()
#binding enter key as adding
root.bind('<Return>',ob.entering)
root.mainloop()
i am not really good at programming language. so I hope I could find a solution from here
The main problem was attempting to reference objects before they had been declared.
Rearranging the code solved that problem.
The other main problem was the importation method used for tkinter
Changing that to a standard form solved that.
Finally i
in class
needed to be changed to self.i
It now works.
import tkinter as tk
root = tk.Tk()
root.title("speed entering calculator")
#creating canvas
canvas1 = tk.Canvas(root, height = 400, width = 400, bg = "black")
canvas1.pack()
#making entry box for values
entry1 = tk.Entry(root, justify = "center")
canvas1.create_window(200, 140, height = 100, width = 100, window = entry1)
value = "the values should be shown here"
label1 = tk.Label(root, text = value, height = 10)
label1.pack()
class Methods1:
i = 0
def entering(self, a):
try:
value1 = int(entry1.get())
value1 = self.i + value1
label1['text'] = value1
self.i = value1
except ValueError as ve:
print(f'only integer')
entry1.delete(0, tk.END) # used to clear entry box
ob = Methods1()
#binding enter key as adding
root.bind('<Return>', ob.entering)
root.mainloop()