函数追溯说未接收位置参数

问题从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,padding determines how mach space is around a window element
diament = tk.Entry()
diament.grid(row = 1,pady = 10)

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

visc = tk.Label(root,text="Fluid Viscosity") # Create a text label
visc.grid(row = 3,padding determines how mach space is around a window element
viscent = tk.Entry()
viscent.grid(row = 3,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转换为float,则控制台将引发无法将字符串转换为float的错误。

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

任何见识都会受到赞赏。

最好, T

jiahua004 回答:函数追溯说未接收位置参数

问题在于您的函数需要参数,但从不使用它们。当您通过按钮调用该函数时,该按钮默认不会传递任何选项。这就是为什么得到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
    ...
本文链接:https://www.f2er.com/3099076.html

大家都在问