Python Forum
Text Entry and Querying CSV - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Text Entry and Querying CSV (/thread-17001.html)



Text Entry and Querying CSV - whacky7 - Mar-24-2019

Hi Guys,

First off, I am new to Python and coding. I do apologise if this question has already been asked. I have searched the forum board and was unable to find a thread that gave me the answer I'm looking for.

Currently writing a small bit of code that will simply query a CSV file for lets say a 'Serial Number'. I have programmed a button that will grab text that is entered in an 'input box' for the user. I now would like to use that entered text to search/query a csv file. I am stuck :(

Cry sorry for being a noob but as mentioned - I am learning and new to Python.

import tkinter as tk
import requests
import csv



root = tk.Tk()

#defining canvas size
HEIGHT = 600
WIDTH  = 800

#defined variables

def get_serial(entry):
    print("Serial Number Has Been Entered:", entry)

#
# with open('testdb.csv') as csvfile:
#    readCSV = csv.reader(csvfile, delimiter=',')


canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

#defining the background image
background_image = tk.PhotoImage(file='background.png')
background_label = tk.Label(root, image=background_image)
background_label.place(relwidth=1, relheight=1)

#defining frame position, color and size
frame = tk.Frame(root, bg='light grey')
frame.place(relx=0.20, rely=0.20, relwidth=0.6, relheight=0.6)

#defining button position and color
button = tk.Button(frame, text="Locate Device", bg='green', command=lambda: get_serial(entry.get()))
button.place(relx=0.25, rely=0.8, relwidth=0.50, relheight=0.10)

#Welcome Screen to db Search
label = tk.Label(frame, text="Welcome to Db Search v1.0 \n written using Python", bg='red')
label.place(relx=0.25, rely=0.05, relwidth=0.50, relheight=0.1)

#defining label for searching the serial number
label = tk.Label(frame, text="Please Enter the Serial Number")
label.place(relx=0.25, rely=0.25, relwidth=0.50, relheight=0.05)

#entry field for Searching via Serial number
entry = tk.Entry(frame, bg='dark grey')
entry.place(relx=0.25, rely=0.30, relwidth=0.50, relheight=0.05)





root.mainloop()

I noticed I may have not explained myself properly on what I would like to achieve.

  1. User inputs 'serial number'
  2. Serial number is then queried against CSV file 'testdb.csv'
  3. Then returns the finding in a text box along with its location and model etc.



RE: Text Entry and Querying CSV - Larz60+ - Mar-24-2019


  1. User inputs 'serial number'
    you need an Entry widget see: https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/entry.html
  2. Serial number is then queried against CSV file 'testdb.csv'
    See: https://docs.python.org/3/library/csv.html
  3. Then returns the finding in a text box along with its location and model etc.
    Need a Text widget see: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/text.html



RE: Text Entry and Querying CSV - whacky7 - Mar-25-2019

Thanks Larz60+,

I have had a read and tried a few functions and still not able to get the results I'm after. It prints every row to the command window. Simply, I need to search a CSV column for the entry (user entered text) and return that row in the text box.


RE: Text Entry and Querying CSV - Larz60+ - Mar-25-2019

You can look at this package I created. It uses many tkinter widgets that you can use for examples.
The GUI part should be good. The remainder perhaps no longer works as It scrapes information that depends on certain web page layouts, and those have probably changes sine I wrote this. But have a look, and see how the various widgets work.
https://github.com/Larz60p/CaliforniaPublicSalaries


RE: Text Entry and Querying CSV - whacky7 - Mar-28-2019

Thanks Larz60+

Your script has helped me a lot.
I will be setting this post as resolved.
I will be making another post regarding SQLite select query that I'm having issues with - hoping you can help out again.