Home > database >  How in Tkinter to transfer variables from the input field to a function from another file for calcul
How in Tkinter to transfer variables from the input field to a function from another file for calcul

Time:08-21

I have an application that is used for fast calculation with substituting periods into a formula from another file

from tkinter import *
from NewDate import  P͞_tic, np

class App(Frame):
    def __init__(self, parent, ):
        Frame.__init__(self, parent, background="black")
        self.parent = parent
        self.p1 = IntVar()
        self.p2 = IntVar()
        self.initUI()
    
    def Calculation(self):
        P1 = np.array([self.p1])
        P2 = np.array([self.p2])
        P͞_tic(P1, P2)

    def initUI(self):
        name_label = Label(text="Period 1")
        surname_label = Label(text="Period 2")
 
        name_label.grid(row=0, column=0, sticky="w")
        surname_label.grid(row=1, column=0, sticky="w")
 
        name_entry = Entry(textvariable=self.p1)
        surname_entry = Entry(textvariable=self.p2)
 
        name_entry.grid(row=0,column=1, padx=5, pady=5)
        surname_entry.grid(row=1,column=1, padx=5, pady=5)
 
 
        message_button = Button(text="Click Me", command=self.Calculation)
        message_button.grid(row=2,column=1, padx=5, pady=5, sticky="e")


    

def main():
    root = Tk()
    #root.geometry("300x250")
    App(root)
    root.mainloop()

if __name__ == '__main__':
    main()

The Calculation function expects two variables that I want to enter through the input fields

For variables I use self.p1 = IntVar() self.p2 = IntVar()

But this causes an error in the imported function 'P͞_tic' Error:

Failed in nopython mode pipeline (step: nopython frontend)
←[1m←[1mnon-precise type array(pyobject, 1d, C)←[0m
←[0m←[1mDuring: typing of argument at C:\Users\neshn\OneDrive\Py_App\NewDate.py (8)←[0m
←[1m
File "NewDate.py", line 8:←[0m
←[1mdef calculating(a, N):
←[1m    x = (2/(N   1))
←[0m    ←[1m^←[0m←[0m

    @jit(nopython=True)
    def calculating(a, N):
        x = (2/(N   1))
        y = 1-(2/(N   1))
        d = np.empty(a.shape)
        d[0] = a[0]
        for i in range(1, a.shape[0]):
            d[i] = a[i] * x   d[i-1] * y 
        return d

        #After I call it in the class functions (P͞_tic)
        df['Ema1'] = calculating(df['Average'].to_numpy(), N)
        df['Ema2'] = calculating(df['Average'].to_numpy(), N1)

Also if I use self.p1 = int instead of self.p1 = IntVar()

The values that I enter do not cause an error in the P_tic functions, but this does not work correctly with Tkinter because the same values are entered into the fields and the calculations in the function are no longer correct

That is, if I want to enter periods 60 and 200, then when I enter 60 in both fields. How do I solve this?

CodePudding user response:

You have to use .get to fetch a value from an IntVar. Hence:

    def Calculation(self):
        P1 = np.array([self.p1.get()])
        P2 = np.array([self.p2.get()])
        P͞_tic(P1, P2)
  • Related