Home > Enterprise >  Calling function from file(1). Try to use variable from 'Entry' input of file(2). end up s
Calling function from file(1). Try to use variable from 'Entry' input of file(2). end up s

Time:02-18

Sorry for the title. I'm not sure how to make it proper for this.

I am scraping website. Most parts are done and now I try to create input fields with TKinter. Using file Button.py to call a function from file Bitkub_scrape.py. Function make_df() needs value of file_path in order to work.

I want to use the input of Entry field in Button.py file. It returns:

FileNotFoundError: [Errno 2] No such file or directory: ''

in which '' is from Bitkub_scrape.py.

Here is my function code in 'Bitkub_scrape.py` file:

def make_df():
    #for loop make list
    for coin, chains, wdf in zip(coin_name_res, chain_name, fee_res):
        #print("Coin name: {} Chain: {} Fee: {}".format(coin, chains, wdf))

        #create dataframe
        df=(pd.DataFrame({'coin_name': coin_name_res[0:100], 'chain_name': chain_name, 'withdrawal_fees':fee_res}))
        #print(df)
        file_path = ""
        #csv
        df.to_csv(file_path, index=False)
        df_saved_file = pd.read_csv(file_path)
        df_saved_file

    driver.quit()

make_df()

And here is my Button.py file:

root = tkinter.Tk()
root.title('Bitkub Fee monitor')
root.geometry("500x500")

e = Entry(root, width=60)
e.pack()

def Bitkub():
    from Bitkub_scrape import make_df
    make_df()
    file_path.append(e.get())
    Bitkub_label = Label(root,text="Done, Check file name: Bitkub_fee.csv")
    Bitkub_label.pack()
    print(path)


Bitkub_button = Button(root,text="Get Bitkub Fee", command=Bitkub, height = 5, width = 60, bg="green")
Bitkub_button.pack()

root.mainloop()

CodePudding user response:

Here's how to implement what I suggested in a comment — passing the Entry's contents to the make_df() function — which requires changes to both source files. Note the below is untested.

Button.py file changes:

from Bitkub_scrape import make_df
import tkinter as tk

root = tk.Tk()
root.title('Bitkub Fee monitor')
root.geometry("500x500")

e = tk.Entry(root, width=60)
e.pack()

def Bitkub():
    make_df(e.get())
    Bitkub_label = Label(root, text="Done, Check file name: Bitkub_fee.csv")
    Bitkub_label.pack()
    print(path)


Bitkub_button = tk.Button(root, text="Get Bitkub Fee", command=Bitkub, height=5, width=60,
                          bg="green")
Bitkub_button.pack()

root.mainloop()

Bitkub_scrape.py file changes:

import pandas as pd

def make_df(file_path):
    for coin, chains, wdf in zip(coin_name_res, chain_name, fee_res):
        #print("Coin name: {} Chain: {} Fee: {}".format(coin, chains, wdf))

        # Create dataframe
        df=(pd.DataFrame({'coin_name': coin_name_res[0:100], 'chain_name': chain_name,
                          'withdrawal_fees':fee_res}))
        #print(df)
        # Create CSV file
        df.to_csv(file_path, index=False)
        df_saved_file = pd.read_csv(file_path)
        df_saved_file

    driver.quit()

Also delete the make_df() call at the end of the file.

  • Related