0

I´m new to python and I have written this code but I´m getting an error I do not know how to resolve, could someone help me, please?

from tkinter import *
def calculations(entries):
  shaftdia = float(entries['Shaft Diameter'].get())
  outdia = 2 * shaftdia
  thickfork = 0.75*shaftdia
  thicksingleye = 1.75*shaftdia
  diapin = shaftdia
  diapincollar = 1.5*shaftdia

  print("Outer diameter of eye: %f" % float(outdia))
  print("Thickness of fork: %f" % float(thickfork))
  print("Thickness of single eye: %f" % float(thicksingleye))
  print("Diameter of pin: %f" % float(diapin))
  print("Diameter of knuckle pin and collar: %f" % float(diapincollar))

 master = Tk()
 Label(master, text="Shaft Diameter").grid(row=0)
 Label(master, text="Outer diameter of eye").grid(row=1)
 Label(master, text="Thickness of fork").grid(row=2)
 Label(master, text="Thickness of single eye").grid(row=3)
 Label(master, text="Diameter of Pin").grid(row=4)
 Label(master, text="Diameter of knuckle pin head and collar").grid(row=5)

 e1 = Entry(master)
 e2 = Entry(master)
 e3 = Entry(master)
 e4 = Entry(master)
 e5 = Entry(master)
 e6 = Entry(master)

e1.grid(row=0, column=50)
e2.grid(row=1, column=50)
e3.grid(row=2, column=50)
e4.grid(row=3, column=50)
e5.grid(row=4, column=50)
e6.grid(row=5, column=50)


Button(master, text='ACCEPT', command=calculations).grid(row=10, column=1, 
sticky=W, pady=4)


master.mainloop( )

It is giving me this error on clicking on the button and I'm unable to resolve the error

Exception in Tkinter callback
Traceback (most recent call last):
File"C:\Users\kunal\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
TypeError: calculations() missing 1 required positional argument: 'entries'

If there is any other error please tell because NOOB IN PYTHON

  • Possible duplicate of [How to pass arguments to a Button command in Tkinter?](https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter) – Tobias Brösamle Feb 20 '19 at 07:00

2 Answers2

1

If you read the exception, you can easily solve it. You defiend calculations function with one input parameter but when you call it you don't pass any parameter to it. Try this code instead of command=calculations:

command = lambda: calculations(YUOR_INPUT_PARAMETER)

For more information see this

Amin
  • 975
  • 8
  • 24
0

The error here is the function command expects entries as an argument, which you have not given on button click.

As you have assigned the text box entries to e1,e2,e3,...,e6 you need not pass entries at all to the command function.

Instead, modify your code like this and it works:

from tkinter import *
def calculations():
  shaftdia = float(e1.get())
  outdia = 2 * shaftdia
  thickfork = 0.75*shaftdia
  thicksingleye = 1.75*shaftdia
  diapin = shaftdia
  diapincollar = 1.5*shaftdia

  print("Outer diameter of eye: %f" % float(outdia))
  e2_var.set(outdia)
  print("Thickness of fork: %f" % float(thickfork))
  e3_var.set(thickfork)
  print("Thickness of single eye: %f" % float(thicksingleye))
  e4_var.set(thicksingleye)
  print("Diameter of pin: %f" % float(diapin))
  e5_var.set(diapin)
  print("Diameter of knuckle pin and collar: %f" % float(diapincollar))
  e6_var.set(diapincollar)
master = Tk()
Label(master, text="Shaft Diameter").grid(row=0)
Label(master, text="Outer diameter of eye").grid(row=1)
Label(master, text="Thickness of fork").grid(row=2)
Label(master, text="Thickness of single eye").grid(row=3)
Label(master, text="Diameter of Pin").grid(row=4)
Label(master, text="Diameter of knuckle pin head and collar").grid(row=5)

e1_var = StringVar()
e2_var = StringVar()
e3_var = StringVar()
e4_var = StringVar()
e5_var = StringVar()
e6_var = StringVar()

e1 = Entry(master,textvariable=e1_var)
e2 = Entry(master,textvariable=e2_var)
e3 = Entry(master,textvariable=e3_var)
e4 = Entry(master,textvariable=e4_var)
e5 = Entry(master,textvariable=e5_var)
e6 = Entry(master,textvariable=e6_var)


e1.grid(row=0, column=50)
e2.grid(row=1, column=50)
e3.grid(row=2, column=50)
e4.grid(row=3, column=50)
e5.grid(row=4, column=50)
e6.grid(row=5, column=50)


Button(master, text='ACCEPT', command=calculations).grid(row=10, column=1, 
sticky=W, pady=4)


master.mainloop( )

Output: (with shaft diameter as 20)

Outer diameter of eye: 40.000000
Thickness of fork: 15.000000
Thickness of single eye: 35.000000
Diameter of pin: 20.000000
Diameter of knuckle pin and collar: 30.000000
Jim Todd
  • 1,488
  • 1
  • 11
  • 15
  • Thanks for the reply!! This worked for me. Could you please tell me how can I print results in the textboxes. – Kunal Shegokar Feb 20 '19 at 07:53
  • Modified the code in answer to display in the UI itself. Please upvote answer if it worked for you. Happy Learning!! – Jim Todd Feb 20 '19 at 09:18
  • can't upvote getting this "Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." sorry – Kunal Shegokar Feb 20 '19 at 14:15
  • this was very helpful. Could you tell me how to add image in the GUI at required location. I've used Pil and pillow and other commands and I've successfully imported the image but it is disturbing other elements of the GUI. I want the image to be in the center and all the text boxes and label should remain aligned as earlier. – Kunal Shegokar Feb 21 '19 at 11:10