Reputation: 51
I am trying to connect my existing Python class to a Tkinter Gui Interface, so I will be able to input variables for existing class in the gui interface. But for some reason my tkinter gui interface is not connected to my existing python class. I have done gui interfaces for python function and everything worked no problem, but with class it's a little bit tricky. It just opens the gui window when I place df = Loan(filename = filename_text.get(), settle_date=settle_date_text.get(), semipmts=semipmts_text.get(), share=share_text.get(), summary_dir=summary_dir_text.get())
after the mainloop or when I place it above the app code,then Python shows an error. I would appreciate any help. Here is my code:
import pandas as pd
import numpy as np
import tkinter as tk
from tkinter import *
class Loan:
def __init__(self, filename, settle_date, semipmts =4,share= 0, summary_dir=0):
#read csv data
self.data=pd.read_csv(filename)
self.filename=filename
self.data=self.data[['month','p','current_upb','current_interest_rate','dq']]
self.settle_date= settle_date
self.semipmts = semipmts
self.summary_dir = summary_dir
def get_data(self):
self.data['Cal_Int'] = self.data['current_interest_rate']/12 *(self.data['month']+self.data['dq']+1)
self.data['Prn'] = self.data['p']*self.data['current_upb']
self.data['Int'] = self.data['Prn'] *self.data['Cal_Int']
self.data['Semi Int'] = self.data['p'] * self.data['current_upb'] * self.data['current_interest_rate']/12 * self.semipmts
self.data.to_csv(self.summary_dir+" summary.csv")
def combined_final_grid(self):
import glob
path = self.summary_dir
files= glob.glob(path + "/*.csv")
data_frame = pd.DataFrame()
content = []
for filename in files:
df = pd.read_csv(filename, index_col=None)
content.append(df)
data_frame = pd.concat(content)
data_frame.head()
data_frame.to_excel(self.summary_dir + "summary.xlsx" , index=False)
app=Tk()
filename_text = StringVar()
filename_label = Label(app, text='File Path "/"', font=('bold', 12), pady=20)
filename_label.grid(row=0, column=0, sticky=W)
filename_entry = Entry(app, textvariable= filename_text)
filename_entry.grid(row=1, column=0)
settle_date_text = StringVar()
settle_date_label = Label(app, text='Settle Date in "Y-m-d"', font=('bold', 12), pady=20)
settle_date_label.grid(row=0, column=1, sticky=W)
settle_date_entry = Entry(app, textvariable = settle_date_text)
settle_date_entry.grid(row=1, column=1)
semipmts_text = DoubleVar()
semipmts_label = Label(app, text=' Pmts Frequency "/"', font=('bold', 12))
semipmts_label.grid(row=0, column=2, sticky=W)
semipmts_entry = Entry(app, textvariable= semipmts_text)
semipmts_entry.grid(row=1, column=2)
share_text = DoubleVar()
share_label = Label(app, text='Sharing', font=('bold', 12), pady=20)
share_label.grid(row=2, column=0, sticky=W)
share_entry = Entry(app, textvariable = share_text)
share_entry.grid(row=3, column=0)
summary_dir_text = StringVar()
summary_dir_label = Label(app, text='Summary Path', font=('bold', 12), pady=20)
summary_dir_label.grid(row=2, column=1, sticky=W)
summary_dir_entry = Entry(app, textvariable = summary_dir_text )
summary_dir_entry.grid(row=3, column=1)
summary_button =Button(app, text='Get Summary', width=15, command = df.get_data())
summary_button.grid(row=15, column=1, pady=20)
app.title('Loan Level')
app.geometry('500x500')
app.mainloop()
df = Loan(filename = filename_text.get(), settle_date=settle_date_text.get(),
semipmts=semipmts_text.get(), share=share_text.get(), summary_dir=summary_dir_text.get())
Upvotes: 0
Views: 45
Reputation: 142859
If you put df = Loan()
before mainloop()
then it runs it before you even see window because mainloop()
creates window and it runs all functions in window - so you will get empty strings from widgets filename_text, settle_date_text, semipmts_text, share_text, summary_dir_text
If you run it after mainloop()
then it runs after you close window and some elements can be destroy()
and remove from memory. And df
will not exists so you can run command=df.get_data
(by the way: it has to be without ()
)
You have to run in function executed by button
def get_summary():
df = Loan(filename=filename_text.get(), settle_date=settle_date_text.get(), semipmts=semipmts_text.get(), share=share_text.get(), summary_dir=summary_dir_text.get())
df.get_data()
# ...code ...
summary_button = Button(app, text='Get Summary', width=15, command=get_summary) # function's name without `()`
Upvotes: 1