Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text Entry and Querying CSV
#1
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.
Reply
#2

  1. User inputs 'serial number'
    you need an Entry widget see: https://infohost.nmt.edu/tcc/help/pubs/t...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/tk.../text.html
Reply
#3
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.
Reply
#4
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
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,361 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Fastest Method for Querying SQL Server with Python Pandas BuJayBelvin 7 6,872 Aug-02-2020, 06:21 PM
Last Post: jefsummers
  Recommendations: Querying CSV / Database?? ironfelix717 2 1,653 Apr-01-2020, 12:13 PM
Last Post: ndc85430
  Querying cvs excel files rturus 6 3,481 Mar-27-2019, 02:08 PM
Last Post: ichabod801
  Querying a web database SimpleGuy 3 3,665 Aug-18-2017, 08:25 PM
Last Post: SimpleGuy

Forum Jump:

User Panel Messages

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