Wouter Wouter - 4 months ago 39
Python Question

Python search CSV file and return result in Tkinter

Question has been updated since I've received an answer.
The question that now raises is how to get the values from the csv file when I have two "Players" next to each other.

from Tkinter import *
import csv

master = Tk()

b1 = StringVar()
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()

b2 = StringVar()
v4 = StringVar()
v5 = StringVar()
v6 = StringVar()

a = Label(master, text="Player 1", font="Verdana 10 bold").grid(row=8, column=1, columnspan=2, pady=15)
b = Label(master, text="Player Name").grid(row=9, column=1, sticky='w')
c = Label(master, text="1st Service Percentage:").grid(row=10, column=1, sticky='w')
cc = Label(master, text="value", textvariable=v1)
d = Label(master, text="Points Won on 1st Serve: ").grid(row=11, column=1, sticky='w')
dd = Label(master, text="value", textvariable=v2)
e = Label(master, text="Points Won on 2nd serve:").grid(row=12, column=1, sticky='w')
ee = Label(master, text="value", textvariable=v3)

a22 = Label(master, text="Player 2", font="Verdana 10 bold").grid(row=8, column=3, columnspan=2, pady=15)
b22 = Label(master, text="Player Name").grid(row=9, column=3, sticky='w')
c22 = Label(master, text="1st Service Percentage:").grid(row=10, column=3, sticky='w')
cc22 = Label(master, text="value", textvariable=v4)
d22 = Label(master, text="Points Won on 1st Serve: ").grid(row=11, column=3, sticky='w')
dd22 = Label(master, text="value", textvariable=v5)
e22 = Label(master, text="Points Won on 2nd serve:").grid(row=12, column=3, sticky='w')
ee22 = Label(master, text="value", textvariable=v6)


def name():
with open("Service.csv") as fh:
for row in fh:
if (b1.get()) in row:
player_name = row[0:row.find(',')]
row = row.replace(player_name+',', '')
firstService = row[0:row.find(",")]
row = row.replace(firstService+',', '')
points_firstserve = row[0:row.find(",")]
row = row.replace(points_firstserve+',', '')
points_secondserve = row[0:row.find(",")]
row = row.replace(points_secondserve+',', '')
v1.set(firstService)
cc.grid(row=10, column=2, sticky='w')
v2.set(points_firstserve)
dd.grid(row=11, column=2, sticky='w')
v3.set(points_secondserve)
ee.grid(row=12, column=2, sticky='w')
if (b2.get()) in row:
player_name = row[0:row.find(',')]
row = row.replace(player_name + ',', '')
firstService = row[0:row.find(",")]
row = row.replace(firstService + ',', '')
points_firstserve = row[0:row.find(",")]
row = row.replace(points_firstserve + ',', '')
points_secondserve = row[0:row.find(",")]
row = row.replace(points_secondserve + ',', '')
v1.set(firstService)
cc.grid(row=10, column=2, sticky='w')
v2.set(points_firstserve)
dd.grid(row=11, column=2, sticky='w')
v3.set(points_secondserve)
ee.grid(row=12, column=2, sticky='w')

myb1 = Entry(master, textvariable=b1)
myb1.insert(10, "Andy Murray")
myb1.grid(row=9, column=2)

myb22 = Entry(master, textvariable=b2)
myb22.insert(10, "Novak Djokovic")
myb22.grid(row=9, column=4)

button1 = Button(master, text='Run', command=name, bg="light green", font="Verdana 9 bold")
button2 = Button(master, text='Quit', command=quit, bg="red", font="Verdana 9 bold")
button1.grid(row=15, column=2, ipadx=50, pady=10)
button2.grid(row=15, column=3, ipadx=50, pady=10, padx=5)

master.geometry("850x500+300+100")
master.bind('<Return>', name)
master.bind('<Escape>', quit)
mainloop()

Answer

Here I am not using v1,v2 and v3 text variables. Appending second player values beside first player values. Enter "Andy Murray" hit Run next Enter "Novak Djokovic" hit run. you will see results of second player beside first player values.

from tkinter import *
import csv

master = Tk()

b1 = StringVar()
#v1 = StringVar()
#v2 = StringVar()
#v3 = StringVar()

a = Label(master, text="Player 1", font="Verdana 10 bold").grid(row=8, column=1, columnspan=2, pady=15)
b = Label(master, text="Player Name").grid(row=9, column=1, sticky='w')
c = Label(master, text="1st Service Percentage:").grid(row=10, column=1, sticky='w')
cc = Label(master, text="")
d = Label(master, text="Points Won on 1st Serve: ").grid(row=11, column=1, sticky='w')
dd = Label(master, text="")
e = Label(master, text="Points Won on 2nd serve:").grid(row=12, column=1, sticky='w')
ee = Label(master, text="")

def name():
    with open("service.csv") as fh:
        for row in fh:
            if (b1.get()) in row:
                player_name = row[0:row.find(',')]
                row=row.replace(player_name+',','')
                #print(row)
                firstService=row[0:row.find(",")]
                row=row.replace(firstService+',','')                
                points_firstserve=row[0:row.find(",")]
                row=row.replace(points_firstserve+',','')                
                points_secondserve=row[0:row.find(",")]
                row=row.replace(points_secondserve+',','')    
                cc['text'] += " "+firstService
                #v1.set(firstService)
                cc.grid(row=10, column=2, sticky='w')
                #v2.set(points_firstserve)
                dd['text'] += " "+points_firstserve
                dd.grid(row=11, column=2, sticky='w')
                #v3.set(points_secondserve)
                ee['text'] += " "+points_secondserve
                ee.grid(row=12, column=2, sticky='w')                

myb1 = Entry(master, textvariable=b1)

myb1.insert(10, "Andy Murray")

myb1.grid(row=9, column=2)

button1 = Button(master, text='Run', command=name, bg="light green", font="Verdana 9 bold")
button2 = Button(master, text='Quit', command=quit, bg="red", font="Verdana 9 bold")

button1.grid(row=15, column=2, ipadx=50, pady=10)
button2.grid(row=15, column=3, ipadx=50, pady=10, padx=5)

master.geometry("850x500+300+100")
master.bind('<Return>', name)
master.bind('<Escape>', quit)
mainloop()