python - TypeError : calculations() missing 1 required positional argument: 'entries'

标签 python tkinter pycharm syntax-error

我是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'

如果还有其他错误,请告诉我们,因为NOOB IN PYTHON

最佳答案

这里的错误是函数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

关于python - TypeError : calculations() missing 1 required positional argument: 'entries' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54780524/

相关文章:

python - 如何在 Windows 上为 PyCharm 配置 Python Kivy?

python - 如何合并具有重复值的字典列表以创建嵌套字典?

python - 信号未从线程传递到 GUI

python - 当套接字循环发生时,Tkinter 无法工作

python - Tkinter - 如何将实例变量传递给另一个类?

python - Jupyter Notebook 在 Pycharm 中无法完全运行

python - 如何使用三重双引号而不是三重单引号制作所有文档字符串?

python - Scipy 优化 : Set maximum error

python - 从 PySpark 中的工作节点访问 ADLS 上的二进制文件的最有效方法?

python - 如何在 Tkinter 文本小部件中配置默认​​的双击鼠标行为?