Python Forum
Get string from entry and use it in another script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get string from entry and use it in another script
#1
Hello, I am trying to insert a file path in an entry in my GUI. I then want to save this entry, as I need the path in another script that I have made. I am however unsure how to do this. I have tried a couple of things but get errors like that the entry is unhashable type, and I am not sure how to fix this. I have read that I should make it to a tuple, but could not make this work either. The part which involves the entry and where I should get the other script to run looks like this :)
#insert filname in entry
fil_udfald_regn = StringVar()
fil_udfald = ttk.Entry(tab1, textvariable=fil_udfald_regn).place(x=145, y=81)
# Definition for udfaldknap
def files_udfald():
    window.filename = filedialog.askopenfilename(initialdir='\\:C', title="Vælg fil",
                                                 filetypes=[("Text files", "*.txt"), ("All files", "*.*")])
    fil_udfald_regn.set(window.filename)
    print(fil_udfald_regn)
    print(fil_udfald)
# Trying to save the entry and then set the script to be equal to entry
def entry_sti(): #todo
    test = gg(fil_udfald_regn.get())
    return fil_udfald_regn.get()

# Button that runs the two definitions 
vlg_udfald = ttk.Button(tab1, text="Vælg udfaldsfil",
                        padding=(2, 2, 2, 2),
                        command=lambda :[files_udfald(),entry_sti()]).place(x=25, y=78)


And the script I am trying to run, when I obtain a file path looks like this
import os
from pathlib import Path
from datetime import datetime
import pandas as pd
from io import StringIO

def gg(name):
    d = pd.read_csv({name}, encoding='ISO-8859-1')
    d.to_csv('Udfald.csv', index=False)
    file1 = open('Udfald.csv', 'r')
    Lines = file1.readlines()

    def convert_udfald():
        # Set start directory same as script
        os.chdir(os.path.abspath(os.path.dirname(__file__)))

        infile = Path('.') / 'Udfald.csv'
        outfile = Path('.') / 'Udfald1.csv'

        with infile.open() as fp, outfile.open('w') as fout:
            for line in fp:
                line = line.strip().split()
                header = 'd'
                # extract header
                if line[0] == 'ååååmmdd':
                    data = f"{header}\n"
                    fout.write(data)
                    Day=int(line[8])
                    #minute= int(line[9])
                    dage = Day
                    Timer= line[9]
                    data = f"{dage}:{Timer}\n"
                    fout.write(data)
            # else:
            # print('false')


    if __name__ == '__main__':
        convert_udfald()

    d = pd.read_csv("Udfald1.csv")
    #print(d)
    t=d.iloc[0,0]
    d=t.strip().split(':')
    dage=float(int(d[0]))
    timer=float(int(d[1]))
    minutter=float(int(d[2]))

    year=(dage+(timer/24)+(minutter/24/60))/365

    year=round(year,ndigits=2)
    print(year)
    df=str(year)
    print(year)

    df=StringIO(df)
    df=pd.read_csv(df)
    df.to_csv('udfald1.csv', index=False)
    df=pd.read_csv('udfald1.csv', header=None)
    print(df.iloc[:1])
The error message i get right now is "ValueError: Invalid file path or buffer object type: <class 'set'>"

The purpose of this is that i want to make my GUI file structured and call script instead of having it all in one fill. This particular example should obtain the path to a txt.file which contains some data, that i want to convert and use later in the GUI
Hope my question makes sense :)
Looking forward to hearing from you!
Reply
#2
create a method within the GUI class to extract (get) the variable from the StringVar
and return it's value.

Then just call that method (instantiate GUI class in 2nd script) from the other script.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: could not convert string to float: '' fron Entry Widget russellm44 5 500 Mar-06-2024, 08:42 PM
Last Post: russellm44
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,427 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,457 Mar-18-2019, 05:36 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020