Python Forum
[Tkinter] Link text field to separate python file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Link text field to separate python file (/thread-2398.html)



Link text field to separate python file - SMA - Mar-13-2017

Hi Guys,

I am trying to take the values of a text field in my GUI.py file, and feed the values in to a search option in Retrieve.py. So when the user types in a username and presses a button, the username they have searched for is then transferred to the retrieve.py file to run searches against a database. I am really not too sure how to do this. I am using Tkinter.

This is my retrieve code:

def username():
   con.sqlite3.connect('database')
    c = conn.cursor
    name = raw_input ("Enter Username")
    c.execute("SELECT users FROM Results WHERE users LIKE '%"+name+"%';")
    data = c.fetchall()
    print (data)
    c.close
    conn.close



RE: Link text field to separate python file - Larz60+ - Mar-13-2017

Do not post code as you did. I added code tags in the proper manner.

The gui code should use a get user button that is bound to the retrieve function
this can be done in the command argument of the button instance or with a separate
bind statement attached to the button. In either case, you will have to provide an event hook
as an argument in retrieve.


RE: Link text field to separate python file - nilamo - Mar-20-2017

.close is a method.  You need parenthases to call it.  c.close() and conn.close().

And please, don't write sql queries like that.  Use the string formatting the db engine provides, so the values are escaped properly (otherwise a "well written" text snippet could "accidentally" delete your entire database).  So maybe
user = "%{}%".format(name)
c.execute("select users from results where users like ?", user)