I am getting an attribute error for any function inside the ButtonClick method. please help in rectifying this.
import tkinter as tk
from tkinter import *
root = tk.Tk()
enter = tk.Entry(root, width = 35, borderwidth = 5).grid(row = 0, column = 0, columnspan = 3, padx =10 , pady = 10)
def ButtonClick(number):
print(number)
current = enter.get()
print(current)
# enter.delete(0,END)
# enter.insert(0, current + number)
button1 = tk.Button(root, text="1", bg="yellow",padx = 40, pady= 20, command=lambda: ButtonClick(1)).grid(row = 1, column = 0)
root.mainloop()
The python shell makes it easy to experiment. Assuming the error is with enter
I just pasted the first part of your code into the shell.
>>> import tkinter as tk
>>> from tkinter import *
>>>
>>> root = tk.Tk()
>>>
>>> enter = tk.Entry(root, width = 35, borderwidth = 5).grid(row = 0, column = 0, columnspan = 3, padx =10 , pady = 10)
>>> repr(enter)
'None'
Yep, its None
. And that's typical. Object methods often don't return their own object. Its convenient for method chaining, but most objects are not designed for that. Just do what examples and tutorials show and put it on a separate line.
import tkinter as tk
from tkinter import *
root = tk.Tk()
enter = tk.Entry(root, width = 35, borderwidth = 5)
enter.grid(row = 0, column = 0, columnspan = 3, padx =10 , pady = 10)
def ButtonClick(number):
print(number)
current = enter.get()
print(current)
# enter.delete(0,END)
# enter.insert(0, current + number)
button1 = tk.Button(root, text="1", bg="yellow",padx = 40, pady= 20, command=lambda: ButtonClick(1)).grid(row = 1, column = 0)
root.mainloop()