amotoli
amotoli

Reputation: 39

How to import a numerical variable inside a function from another file - Python

I'm trying to create a welding throat calculator just to practice my Python skills. I have 2 files for the same project, throat_size.py and support.py. I use support.py, just to make the calculus that I need, and I want to call these results throat_size, but I'm getting: name 'a' is not defined.

throat_size.py:

import tkinter as tk
import support as sp


firstWindowResults = []

firstWindow = tk.Tk()

w = 650
p = 470

ws = firstWindow.winfo_screenwidth() # width of the screen
hs = firstWindow.winfo_screenheight() # height of the screen
x = (ws/2) - (w/2)
y = (hs/2) - (p/2)

firstWindowTitle = tk.Label(firstWindow, text="Cordões de soldadura")
firstWindowTitle.grid(column=1, row=0, padx=10, pady=10)

firstInput = tk.Label(firstWindow, text="Menor espessura a soldar")
firstInput.grid(column=0, row=3, padx=10, pady=10)

espessura = tk.Entry(firstWindow)
espessura.grid(column=1, row=3, padx=10, pady=10)

firstWindowValues = [espessura]



def validateFirstWindowValues():
    invalid = 0
    firstWindowResults.clear()
    for value in firstWindowValues:
        if value == espessura:
            if float(value.get()) <= 0:
                entry = firstWindowValues.index(value) + 1
                tk.messagebox.showerror("Valor inválido", "Ver linha  " + str(entry) + "!")
                invalid += 1
                break
            else:
                firstWindowResults.append(value.get())

    if invalid == 0:
        sp.thickness(firstWindowResults[0])


Res = "a = " + sp.a1

#Passo 3: Loop
nextButton = tk.Button(firstWindow, text="Enter", command=validateFirstWindowValues)
nextButton.grid(column=1, row=9, padx=10, pady=10)

firstWindow.geometry('%dx%d+%d+%d' % (w, p, x, y))
firstWindow.mainloop()

support.py:

import math as mt

def thickness(espessura):
   global a1, rz1, z1, a2, rz2, z2
   rz1, rz2 = 0.5, 0.7

   a1 = float(espessura) * rz1
   z1 = 2 * mt.cos(mt.radians(45)) * a1

   a2 = float(espessura) * rz2
   z2 = 2 * mt.cos(mt.radians(45)) * a2
    

Upvotes: 0

Views: 96

Answers (1)

8349697
8349697

Reputation: 605

The function must return values that can be used.

import math as mt


def thickness(espessura):
    """
    espessura: string
    return a tuple of numbers: a1, rz1, z1, a2, rz2, z2, esp
    
    >>> # unpacking values:
    >>> a1, rz1, z1, a2, rz2, z2, esp = thickness("2")
    >>> a1
    1.0
    >>> z1
    1.4142135623730951
    >>> rz1
    0.5
    >>> # or:
    >>> thickness_values = thickness("2")
    >>> thickness_values[0] # a1
    1.0
    >>> thickness_values[6] # espessura
    2.0
    """
    rz1, rz2 = 0.5, 0.7
    esp = float(espessura)
    
    a1 = esp * rz1
    z1 = 2 * mt.cos(mt.radians(45)) * a1

    a2 = esp * rz2
    z2 = 2 * mt.cos(mt.radians(45)) * a2

    return a1, rz1, z1, a2, rz2, z2, esp

An abbreviated code example for using a function.

import tkinter as tk
import support as sp


firstWindow = tk.Tk()

w = 650
p = 470

ws = firstWindow.winfo_screenwidth() # width of the screen
hs = firstWindow.winfo_screenheight() # height of the screen
x = (ws/2) - (w/2)
y = (hs/2) - (p/2)

firstWindowTitle = tk.Label(firstWindow, text="Cordões de soldadura")
firstWindowTitle.grid(column=1, row=0, padx=10, pady=10)

firstInput = tk.Label(firstWindow, text="Menor espessura a soldar")
firstInput.grid(column=0, row=3, padx=10, pady=10)

espessura = tk.Entry(firstWindow)
espessura.grid(column=1, row=3, padx=10, pady=10)


def validateFirstWindowValues():
    a1, rz1, z1, a2, rz2, z2, esp = sp.thickness(espessura.get())
    print(a1, rz1, z1, a2, rz2, z2, esp)
    espessuraLabel["text"] = f"espessura: {esp}"
    firstLabel["text"] = f"rz1: {rz1}, a1: {a1}, z1: {z1}"
    secondLabel["text"] = f"rz2: {rz2}, a2: {a2}, z2: {z2}"

      
resultsFrame = tk.Frame(firstWindow)
resultsFrame.grid(column=0, row=10, columnspan=2 , padx=10, pady=10)
espessuraLabel = tk.Label(resultsFrame, text="")
espessuraLabel.grid(column=0, row=0)
firstLabel = tk.Label(resultsFrame, text="")
firstLabel.grid(column=0, row=1)
secondLabel = tk.Label(resultsFrame, text="")
secondLabel.grid(column=0, row=2)

#Passo 3: Loop
nextButton = tk.Button(firstWindow, text="Enter", command=validateFirstWindowValues)
nextButton.grid(column=1, row=9, padx=10, pady=10)

firstWindow.geometry('%dx%d+%d+%d' % (w, p, x, y))
firstWindow.mainloop()

Upvotes: 1

Related Questions