Home > database >  How to pass user data from an entry widget to verify the data in another script?
How to pass user data from an entry widget to verify the data in another script?

Time:12-21

I'm trying to retrieve a variable from an entry from script 1 and pass it to script 2 where it will run a function to check if the inputted data matches certain requirements.

Script 1 is a form GUI where a user would enter things such as 'Product Name', 'Version', 'Email', etc. Then it would pass the entries into script 2 into an if statement that would check if the version was entered in a #.## format, if it only contains [0-9], and a [.]. If not, it would pop up a warning saying the field was entered incorrectly and stop the script.

I looked around and have found similar questions and solutions, but I couldn't get any of them to solve my issue.

I simplified the script to only include the relevant info, I kept version as an example, but can add more if needed. I did the same for script 2 to just do a check to see if the user entered anything. I figured if I was able to get the script to print 'Empty' if no data was entered, adding a warning popup wouldn't be too hard to include in.

Script1.py:

from tkinter import *
from script2 import *
root = Tk()

def startCheck():
    checkEntries()

# Label1, Label 2, etc
entry_version= Entry(root, width=10)
entry_version.grid(row=1,column=1)

#rest of the entries follow the same structure
btn = Button(root, text="start Check", command=startCheck)
btn.grid(row=2, column=1)

script2.py:

def checkEntries():
    if len(entry_version.get()) == 0:
        print("Empty")

However, this brought up a warning stating that entry_version is not defined. After doing some research I found out that each script when initialized creates their own variable data, so there are two entry_version running and they are completely different. I thought I could make the change to startCheck() to:

def startCheck():
    test.checkEntries()

but the result was the same, saying that test is not defined.

My next thought was to add import script1 into script2 in order to avoid two different variables. This didn't work as it just created two instances of the GUI and told me that test is not defined again.

Lastly, I discovered wrapping the entirety of script1, minus the imports, in if __name__ == '__main__': does stop the import from script2 from creating another instance of script1, but I'm still at the exact same point as before.

I hope this wasn't confusing, and I am more than happy to explain this differently if it is confusing.

How can I get this to work with two scripts? I want to avoid adding the checkEntries function into script1 if it is possible

CodePudding user response:

Perhaps use lambda.

def check_entry(event, entry):
   print(entry.get())

myEntry = Entry(root)
myEntry.grid(row=1,column=1)

Button(root, text="check", command=lambda e=Event(), me=myEntry: check_entry(e, me)).grid(row=2,column=1)

Note: lambda can generate Event as the first arg. I have always chosen to include it just to be safe and stop future issue.

if you want to check multiple entries use a list of StringVar() and assign them to 'textvariable' for the Entry

def check_entries(event, txtvars):
   for t in txtvars:
       print(t.get())

txtvars = [StringVar()]*10 #creates list of 10 StringVar()
for index, t in enumerate(txtvars): ##loops through the list setting t to the item (using index 1 for row)
    Entry(root, textvariable=t).grid(row=index 1,column=1)

Button(root, text="check", command=lambda e=Event(), tx=txtvars: check_entry(e, tx)).grid(row=2,column=1)

to pass it to the other script just replace the print() with your imported method name, e.g. checkEntries(entry.get())

  • Related