【问题标题】:Function traceback says not receiving positional arguments函数回溯说不接收位置参数
【发布时间】:2020-03-11 00:39:08
【问题描述】:

问题从 def displayAnswer 开始

import tkinter as tk

#icftk stands for incompressible-flow toolkit

"""This program is being built to aid in getting all 
parameters of a flow given certain initial conditions"""

#CREATES THE WINDOW
root = tk.Tk()
root.title("Incompressible Fluid Toolkit")

class flow:
    """This class contains all necessary parameters needed to define a flow that i 
    incompressible, 1D-Steady, idiabatic and encounters no energy gain or loss"""
    def __init__(self,vel,diameter,density,viscosity,massflow = 0, Re = 0, newdia = 1, jetforce = 0, newvel = 0):
        """initialize a fluid with given basic measured properties"""
        self.vel = vel
        self.diameter = diameter
        self.density = density
        self.viscosity = viscosity
        self.massflow = massflow # mass flow rate
        self.Re = Re #Reynolds Number
        self.newdia = newdia # downstream diameter for velocity change
        self.jetforce = jetforce # force the stream can produce normal to a surface
        self.newvel = newvel # new velocity after a cross sectional area change

    def reynolds(self):
        """This function calculates reynolds
        Pass ro, v, D and mu in the same unit system, in that order"""
        self.Re = (self.diameter*self.vel*self.density)/(self.viscosity)
        print(f"The Reynolds number for this flow is {self.Re}")

    def mdot(self):
        """This function finds the mass flowrate of a flow"""
        flowarea = 3.14159*(self.diameter**2) / 4
        self.massflow = self.density*self.vel*flowarea
        print(f"The mass flowrate is {self.massflow}")

    def streamforce(self):
        """This function gives the max force that the fluid jet can apply 
        normal to a surface perpendicular to the flow"""
        self.jetforce = self.massflow*self.vel
        print(f"The maximum force the jet can apply is {self.jetforce}")

    def velchange(self):
        """This function is used to determine the velocity change of 
        a flow when there is a change in cross sectional area of the pipe"""
        newarea = 3.14159 * (self.newdia**2) / 4
        self.newvel = self.massflow/(self.density*newarea)
        print(f"At the location of the area change, there is a velocity change from {self.vel} to {self.newvel}")
    #ALL ABOVE FUNCTIONS HAVE BEEN CONFIRMED TO WORK WITH GIVEN TEST CONDITIONS BELOW

#use test case velocity = 18.64, diameter = 0.017, density = 1.23, and viscosity = 0.0000184

#Display Entry Boxes

velo = tk.Label(root, text="Flow Velocity") # Create a text label
velo.grid(row = 0, column = 0, pady = 10) # Pack it into the window, padding determines how mach space is around a window element
veloent = tk.Entry()
veloent.grid(row = 0, column = 1, pady = 10)

diam = tk.Label(root, text="Pipe Diameter") # Create a text label
diam.grid(row = 1, column = 0, pady = 10) # Pack it into the window, padding determines how mach space is around a window element
diament = tk.Entry()
diament.grid(row = 1, column = 1, pady = 10)

dens = tk.Label(root, text="Fluid Density") # Create a text label
dens.grid(row = 2, column = 0, pady = 10) # Pack it into the window, padding determines how mach space is around a window element
densent = tk.Entry()
densent.grid(row = 2, column = 1, pady = 10)

visc = tk.Label(root, text="Fluid Viscosity") # Create a text label
visc.grid(row = 3, column = 0, pady = 10) # Pack it into the window, padding determines how mach space is around a window element
viscent = tk.Entry()
viscent.grid(row = 3, column = 1, pady = 10)

#Display answers at the bottom of the window

def displayAnswer(veloent,diament,densent,viscent):
    ve = float(veloent)#gets velocity entry and turns it into a float
    di = float(diament)#gets diameter entry and turns it into a float
    de = float(densent)#gets density entry and turns it into a float
    vi = float(viscent)#gets viscosity entry and turns it into a float

    fluid = flow(ve,di,de,vi)
    fluid.reynolds()
    fluid.mdot()
    fluid.streamforce()

    reynoldsanswer = tk.Label(root, text = "f{fluid.reynolds}")
    reynoldsanswer.grid(row = 5)
    mdotanswer = tk.Label(root, text = "f{fluid.mdot}")
    mdotanswer.grid(row = 6)
    streamforceanswer = tk.Label(root, text = "f{fluid.streamforce}")
    streamforceanswer.grid(row = 7)

calculatebutton  = tk.Button(root,command = displayAnswer)
calculatebutton.grid(row = 4)

root.mainloop()

我是 tkinter 的新手,试图获得设计简单 GUI 的经验。我正在使用一个按钮来启动计算以获取有关不可压缩流的值。按下按钮时,控制台会抛出此错误。

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
TypeError: displayAnswer() missing 4 required positional arguments: 'veloent', 'diament', 'densent', and 'viscent'

同样,如果我尝试将 Entry 从函数外部的字符串转换为浮点数,控制台会抛出无法将字符串转换为浮点数的错误。

老实说,我不确定所有代码是否都正确,但我会一次跨过这些桥梁。

感谢任何见解。

最好, T

【问题讨论】:

  • 欢迎来到 Stack Overflow!查看tour。如果您创建了minimal reproducible example,将会有所帮助。如果您想了解更多提示,请参阅How to Ask
  • 错误告诉你问题出在哪里——你的函数需要四个参数,但是当你从按钮调用命令时,它不会发送任何参数。既然您要获取所需的值,为什么您的函数需要参数?
  • @BryanOakley 老实说,我什至不认为我可以编写一个不带参数的函数,谢谢!

标签: python-3.x user-interface tkinter typeerror


【解决方案1】:

问题是您的函数需要参数,但从不使用它们。当您从按钮调用该函数时,该按钮默认不会传递任何选项。这就是你得到missing 4 required positional arguments 的原因 - 该函数需要四个,按钮传递为零。

由于您的函数实际上在做正确的事情并获取所需的值,因此无需传入参数。简单的解决方法是简单地将它们从函数的定义中删除:

def displayAnswer():
    ve = float(veloent)#gets velocity entry and turns it into a float
    di = float(diament)#gets diameter entry and turns it into a float
    de = float(densent)#gets density entry and turns it into a float
    vi = float(viscent)#gets viscosity entry and turns it into a float
    ...

【讨论】:

    猜你喜欢
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 2014-09-30
    • 1970-01-01
    相关资源
    最近更新 更多