0

Im having trouble calling an variable from a function within another function.

I have two functions:

def refineRate(event):

    if refineRate(event) == int():
        refine = int(refineRate(event))
        return refine

    else:
        refine = float(refineRate(event))
        return refine

and:

def veldCalc(event):

    minValue = open("mineral_value.csv", "r")
    veld = minValue.readlines()[0]
    minValue.close()
    veld = veld[0:3]
    veld = int(veld)
    veld = veld / 100 * refineRate(event)
    refinedVeld = veld * int(veldCalc)
    print (refinedVeld)

I also have two entry where the user of the calculator can enter some figures.

repro = Label(root, text="Reprocessing %")
repro_entry = Entry(root)
repro.grid(row=0, column=0)
repro_entry.grid(row=0, column=1)


veld = Label(root, text="Veldspar: ")
veld_entry = Entry(root)
veld.grid(row=1, column=0)
veld_entry.grid(row=1, column=1)

repro_entry.bind("<KeyPress>", refineRate)
veld_entry.bind("<KeyPress>", veldCalc)

What i need is for the user to input there refineRate and which should then get passed through the function and stored for later use. the when they enter the amount they have, the veldCalc function should then pull the users refine rate from the previous function and do the math but im getting the following errors

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Ganjena\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:/Users/Ganjena/Desktop/Course/Projects/helloworld/ORE FUNCTIONS/Ore Calculator.py", line 5, in refineRate
    if refineRate(event) == int():
  File "C:/Users/Ganjena/Desktop/Course/Projects/helloworld/ORE FUNCTIONS/Ore Calculator.py", line 5, in refineRate
    if refineRate(event) == int():
  File "C:/Users/Ganjena/Desktop/Course/Projects/helloworld/ORE FUNCTIONS/Ore Calculator.py", line 5, in refineRate
    if refineRate(event) == int():
  [Previous line repeated 990 more times]
RecursionError: maximum recursion depth exceede

any idea to why this isn't working? Thank you in advance.

Gromit
  • 77
  • 8
  • 2
    The recursion in your first function will never stop, you always have a recursive call in your `if` condition... – juanpa.arrivillaga Apr 09 '17 at 20:08
  • ok i think i understand, and how could i stop this? but its not a loop so why does it continue to run itself? – Gromit Apr 09 '17 at 20:10
  • Because it's a recursive function that always calls itself, like try `def f(): f()` You never reach your else block because each time you call the function, it checks the `if` condition, and calls itself again... endlessly (well, until Python errors you out). And indeed, you **always** end up using a recursive call... so I don't know what you expect to happen or what your function is usppose to do. – juanpa.arrivillaga Apr 09 '17 at 20:11
  • all i want it to do is take the user input check if its a `int` or a `float` and then store it to be used in the `veldCalc` funtion – Gromit Apr 09 '17 at 20:15
  • The rest of your code is very strange as well. For example, in your second function, what do you expect `veld * int(veldCalc)` to do? `veldCalc` is the function itself! – juanpa.arrivillaga Apr 09 '17 at 20:18
  • If you want t check if a variable refers to an `int` or `float` the use the built-in `isinstance()` – cdarke Apr 09 '17 at 20:21
  • hmm i haven't got round to learning about `isinstance` ill look into it. the `veld * int(veldCalc)` should be the `veld` taken from the file * the users input which is bound to that function – Gromit Apr 09 '17 at 20:25
  • basically, what i want there is the user to input how much veld they have and then that `veldCalc` function first takes the base rate of veld, subtracts the `refineRate` value and then multiply it by the users input (which is binded to the `veldCalc` function) – Gromit Apr 09 '17 at 20:33

1 Answers1

0

You seem to have a misconception of how to get the user-input value inside a tk.Entry widget. The proper way is to use the .get() method on the widget.

Here is a minimal example to get you started:

# (Use Tkinter/tkinter depending on Python version)
import Tkinter as tk 

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.repro = tk.Label(self, text="Reprocessing %")
        self.repro_entry = tk.Entry(self)
        self.repro.grid(row=0, column=0)
        self.repro_entry.grid(row=0, column=1)
        self.repro_entry.bind("<Return>", self.refineRate)
        self.mainloop()

    def refineRate(self, evt):
        # Get the user input and print it in the terminal
        userRepro = self.repro_entry.get()
        print(userRepro)

# Launch the GUI
app = App()

Then, to check whether the user input is a float or an int, you can use the technique described in this link.

Community
  • 1
  • 1
Josselin
  • 2,593
  • 2
  • 22
  • 35