TypeError:calculate()缺少1个必需的位置参数:'entries'

问题描述 投票:0回答:2

我是python的新手,我已经编写了这段代码但是我收到了一个错误,我不知道如何解决,有人可以帮帮我吗?

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( )

点击按钮给我这个错误,我无法解决错误

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'

如果有任何其他错误,请告诉因为在PYTHON的NOOB

python tkinter pycharm syntax-error
2个回答
0
投票

这里的错误是函数command期望entries作为参数,你没有在button点击上给出。

由于您已将文本框条目分配给e1,e2,e3,...,e6,因此您无需将entries传递给command函数。

相反,像这样修改你的代码,它的工作原理:

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( )

输出:(轴直径为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

0
投票

如果您阅读了例外,您可以轻松解决它。你使用一个输入参数来定义calculations函数但是当你调用它时你不会传递任何参数。尝试使用此代码而不是command=calculations

command = lambda: calculations(YUOR_INPUT_PARAMETER)

有关see this的更多信息

© www.soinside.com 2019 - 2024. All rights reserved.