Building a Dynamic Cricket Scoreboard with Python Tkinter


Building a Dynamic Cricket Scoreboard with Python Tkinter

Introduction

Cricket is not just a sport; it’s a passion for millions around the world. Whether you’re a developer with a love for cricket or simply interested in enhancing your Python skills, building a dynamic cricket scoreboard using Python’s Tkinter library is a rewarding project. In this blog, I’ll walk you through the process of creating a simple yet effective cricket scoreboard GUI, complete with data persistence using CSV files.

Why Tkinter?

Tkinter is the standard GUI toolkit for Python. It’s easy to use, comes pre-installed with Python, and allows you to create functional and visually appealing interfaces quickly. For this project, Tkinter is an ideal choice due to its simplicity and flexibility.

Project Overview

In this project, we’ll create a cricket scoreboard application that allows users to input details such as the striker, non-striker, scores, overs, and bowler stats. The application will then display this data in a formatted scoreboard. Additionally, the data will be saved to a CSV file for future reference.

Key Features

  • User Input: Fields for entering details like striker, non-striker, team names, scores, and overs.
  • Scoreboard Display: A real-time display of the scoreboard based on the input.
  • Data Persistence: Save the scoreboard data to a CSV file and retrieve it later.

Step-by-Step Guide

1. Setting Up the Environment

First, ensure you have Python installed. Tkinter is included with most Python installations, so no additional packages are needed.

pip install python-tk

2. Designing the User Interface

We start by creating a window with input fields for various cricket-related details. Here’s a snippet to initialize the main window:

from tkinter import *

window = Tk()
window.geometry("1500x690")
window.title("Cricket Scoreboard")
window.config(background="skyblue", padx=30, pady=50)

Next, we’ll add labels and entry fields for each piece of data we want to capture (striker, score, overs, etc.). For instance, the striker’s details:

labelstriker = Label(window, width=22, text="STRIKER SHIRT NO.:", font=("Arial", 20), bg="black", fg="white", relief=RIDGE, bd=10)
labelstriker.place(x=0, y=10)
strikervalue = Entry(window, font=("Arial", 20), fg="white", bg="red", bd=10, relief=RIDGE)
strikervalue.place(x=370, y=10)

3. Implementing the Logic

The logic of the scoreboard is handled by the scoreboard() function, which:

  • Reads the current input values.
  • Saves them to a CSV file.
  • Updates the scoreboard display dynamically.

Here’s how we manage data input and updating:

def scoreboard():
    striker = strikervalue.get()
    strikerscore = strikerscorevalue.get()
    # Other data inputs...

    listinfo = [striker, strikerscore, nonstriker, nonstrikerscore, team1, over, bowler]

    # Save to CSV
    with open("scoreinfo.csv", "a", newline='') as fh:
        writer = csv.writer(fh)
        writer.writerow(listinfo)

    # Update the scoreboard display...

4. Managing Data Persistence

We use a CSV file to store and retrieve the last entered data. This allows the application to remember the previous game’s data when restarted. The read() function handles this:

def read():
    with open("scoreinfo.csv", "r") as fh:
        reader = csv.reader(fh)
        return list(reader)[-2]  # Retrieve the last entry

5. Finalizing the Scoreboard Display

The application displays the scoreboard in a new window with labels for each field. The data is automatically updated whenever the user submits new information.

def scoreboard():
    # Logic to update the GUI elements
    label1.config(text=striker)
    label2.config(text=strikerscore)
    # Additional updates...

Conclusion

Building a cricket scoreboard using Python and Tkinter is an excellent way to practice GUI development, data handling, and logic implementation. With this project, you can not only create a functional application but also learn how to manage data persistence with CSV files.

Whether you’re a Python enthusiast or a cricket fan, this project offers a fun and educational experience. Try extending the project by adding new features like tracking more statistics or providing historical game data analysis.

Call to Action

Feel free to download the source code and start customizing your own cricket scoreboard. Let’s keep the spirit of cricket alive, one code at a time!

STAY TUNED FOR FURTHER UPDATES


from tkinter import *
window1=None
window=None
label1=None
label2=None
label3=None
label4=None
label5=None
label6=None
label8=None
label9=None
label10=None
label11=None
label13=None
def scoreboard():
    global scoreshow
    global strikervalue
    striker=strikervalue.get()
    
    global strikerscorevalue
    strikerscore=strikerscorevalue.get()
    
    global nonstrikervalue
    nonstriker=nonstrikervalue.get()
    
    global nonstrikerscorevalue
    nonstrikerscore=nonstrikerscorevalue.get()
    
    global team1value
    team1=team1value.get()
    
    

    global overvalue
    over=overvalue.get()
    
    global bowlervalue
    bowler=bowlervalue.get()
    
    global bowlerstatsvalue
    bowlerstats=bowlerstatsvalue.get()
    
    global overstatsvalue
    overstats=overstatsvalue.get()
    
    global notevalue
    note=notevalue.get()
    
    global impactvalue
    impact=impactvalue.get()

    global team1scoreteam2value
    team1scoreteam2=team1scoreteam2value.get()
    #scoreshow+=impact
    

    global team2value
    team2=team2value.get()



    global label1,label2,label3,label4,label5,label6,label8,label9,label10,label11,label13
    listinfo=[striker,strikerscore,nonstriker,nonstrikerscore,team1,
              team1scoreteam2
              ,over,bowler,bowlerstats,
              overstats,note,impact,team2]
    if striker=="45":
        striker="ROHIT"
    elif striker=="18":
        striker="VIRAT"
    if nonstriker=="45":
        nonstriker="ROHIT"
    elif nonstriker=="18":
        nonstriker="VIRAT"
    if bowler=="10":
        bowler="SHAHEEN"
    elif bowler=="71":
        bowler="NASEEM"
    import csv
    fh=open("scoreinfo.csv","a")
    fo=csv.writer(fh)
    fo.writerow(listinfo)
    fh.close()

    global window1
    
    new=read()
    #print(new)
    #print(new[11])    
    if new[11]=="1" or new[11]=="3" or new[11]=="0" and new[6][-1]=="6":
        k=nonstrikervalue
        p=nonstrikerscorevalue
        nonstrikervalue=strikervalue
        nonstrikerscorevalue=strikerscorevalue
        strikervalue=k
        strikerscorevalue=p

    if window1 is not None:
        label1.config(text=striker)
        label2.config(text=strikerscore)
        label3.config(text=nonstriker)
        label4.config(text=nonstrikerscore)
        label5.config(text=team1)
        label6.config(text=team1scoreteam2)
        label8.config(text=over)
        label9.config(text="VS "+team2)
        label10.config(text=bowler)
        label11.config(text=bowlerstats)
        label13.config(text=overstats)
    else:
        if note=="-": 
            window1=Tk()
            window1.geometry("1300x190")
            window1.config(relief=RIDGE,background="#882626",bd=20,padx=90)
            label1=Label(window1,width=10,text=striker,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9,)
            label1.place(x=0,y=30)
            label2=Label(window1,width=5,text=strikerscore,font=("Arial",22),fg="blue",relief=RIDGE,bd=9,)
            label2.place(x=200,y=30)

            label3=Label(window1,width=10,text=nonstriker,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9)
            label3.place(x=0,y=80)
            label4=Label(window1,width=5,text=nonstrikerscore,font=("Arial",22),fg="blue",relief=RIDGE,bd=9,)
            label4.place(x=200,y=80)

            label5=Label(window1,width=5,text=team1,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9)
            label5.place(x=335,y=30)
            label6=Label(window1,width=7,text=team1scoreteam2,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9,)
            label6.place(x=445,y=30)

            label7=Label(window1,width=5,text="OVER",font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9)
            label7.place(x=335,y=80)
            label8=Label(window1,width=2,text=over,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9,)
            label8.place(x=445,y=80)
            label9=Label(window1,width=8,height=2,text="VS "+team2,font=("Arial",12),fg="blue",relief=RIDGE,bd=9,)
            label9.place(x=500,y=80)
            

            label10=Label(window1,width=10,text=bowler,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9)
            label10.place(x=620,y=30)
            label11=Label(window1,width=10,text=bowlerstats,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9,)
            label11.place(x=821,y=30)

            label12=Label(window1,width=10,text="THIS OVER",font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9)
            label12.place(x=620,y=80)
            label13=Label(window1,width=10,text=overstats,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9,)
            label13.place(x=821,y=80)
            
        else:
            window1=Tk()
            window1.geometry("1300x190")
            window1.config(relief=RIDGE,background="#882626",bd=20,padx=90)
            label=Label(window1,width=10,text=striker,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9,)
            label.place(x=0,y=30)
            label=Label(window1,width=5,text=strikerscore,font=("Arial",22),fg="blue",relief=RIDGE,bd=9,)
            label.place(x=200,y=30)

            label=Label(window1,width=10,text=nonstriker,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9)
            label.place(x=0,y=80)
            label=Label(window1,width=5,text=nonstrikerscore,font=("Arial",22),fg="blue",relief=RIDGE,bd=9,)
            label.place(x=200,y=80)

            label=Label(window1,width=5,text=team1,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9)
            label.place(x=335,y=30)
            label=Label(window1,width=7,text=team1scoreteam2,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9,)
            label.place(x=445,y=30)

            label=Label(window1,width=5,text="OVER",font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9)
            label.place(x=335,y=80)
            label=Label(window1,width=2,text=over,font=("Arial",22,"bold"),fg="blue",relief=RIDGE,bd=9,)
            label.place(x=445,y=80)
            label=Label(window1,width=8,height=2,text="VS "+team2,font=("Arial",12),fg="blue",relief=RIDGE,bd=9,)
            label.place(x=500,y=80)

            label=Label(window1,width=22,height=3,text=note,font=("Arial",17,"bold"),fg="blue",relief=RIDGE,bd=9)
            label.place(x=620,y=30)
        
        
    
        







        
        

        
def read():
    import csv
    fh=open("scoreinfo.csv","r")
    fo=csv.reader(fh)
    fo=list(fo)
    new=fo[-2]
    print(fo[-2])
    return new
    #print(fo[11])
    print(new)
    fh.close()


new=read()
strikervalue=new[0]
strikerscorevalue=new[1]
nonstrikervalue=new[2]
nonstrikerscorevalue=new[3]
team1value=new[4]
team1scoreteam2value=new[5]
overvalue=new[6]
bowlervalue=new[7]
bowlerstatsvalue=new[8]
overstatsvalue=new[9]
notevalue=new[10]
impactvalue=new[11]
team2value=new[12]
if new[11]=="1" or new[11]=="3" or new[11]=="0" and new[6][-1]=="6":
        k=nonstrikervalue
        p=nonstrikerscorevalue
        nonstrikervalue=strikervalue
        nonstrikerscorevalue=strikerscorevalue
        strikervalue=k
        strikerscorevalue=p
window=Tk()
window.geometry("1500x690")
window.title("scoreboard info entry")
window.config(background="skyblue",padx=30,pady=50)
labelstriker=Label(window,width=22,text="STRIKER SHIRT NO.:",font=("Arial",20),bg="black",fg="white",relief=RIDGE,bd=10)
labelstriker.place(x=0,y=10)
strikervalue=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
strikervalue.insert(0,new[0])
strikervalue.place(x=370,y=10)

labelstrikerscore=Label(window,width=22,text="STRIKER SCORE:",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labelstrikerscore.place(x=0,y=80)
strikerscorevalue=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
strikerscorevalue.insert(0,new[1])
strikerscorevalue.place(x=370,y=80)

labelnonstriker=Label(window,width=22,text="NON-STRIKER SHIRT NO.:",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labelnonstriker.place(x=0,y=160)
nonstrikervalue=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
nonstrikervalue.insert(0,new[2])
nonstrikervalue.place(x=370,y=160)

labelnonstrikerscore=Label(window,width=22,text="NON-STRIKER SCORE:",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labelnonstrikerscore.place(x=0,y=240)
nonstrikerscorevalue=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
nonstrikerscorevalue.insert(0,new[3])
nonstrikerscorevalue.place(x=370,y=240)

labelteam1=Label(window,width=22,text="BATTING TEAM NAME:",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labelteam1.place(x=0,y=320)
team1value=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
team1value.insert(0,new[4])
team1value.place(x=370,y=320)

labelteam2=Label(window,width=22,text="BOWLING TEAM NAME:",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labelteam2.place(x=0,y=400)
team2value=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
team2value.insert(0,new[12])
team2value.place(x=370,y=400)

labelimpact=Label(window,width=22,text="IMPACT OF BALL BOLWED:",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labelimpact.place(x=0,y=480)
impactvalue=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
impactvalue.insert(0,new[11])
impactvalue.place(x=370,y=480)

labelteam1score=Label(window,width=23,text="BATTING TEAM SCORE:",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labelteam1score.place(x=720,y=10)
team1scoreteam2value=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
team1scoreteam2value.insert(0,new[5])
team1scoreteam2value.place(x=1110,y=10)

labelover=Label(window,width=23,text="OVER     :",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labelover.place(x=720,y=80)
overvalue=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
overvalue.insert(0,new[6])
overvalue.place(x=1110,y=80)

labelbowler=Label(window,width=23,text="BOWLER SHIRT NO.:",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labelbowler.place(x=720,y=160)
bowlervalue=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
bowlervalue.insert(0,new[7])
bowlervalue.place(x=1110,y=160)

labelbowlerstats=Label(window,width=23,text="BOWLER STATS:",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labelbowlerstats.place(x=720,y=240)
bowlerstatsvalue=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
bowlerstatsvalue.insert(0,new[8])
bowlerstatsvalue.place(x=1110,y=240)

labeloverstats=Label(window,width=23,text="OVER STATS:",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labeloverstats.place(x=720,y=320)
overstatsvalue=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
overstatsvalue.insert(0,new[9])
overstatsvalue.place(x=1110,y=320)

labelnote=Label(window,width=23,text="NOTE::",font=("Arial",20),fg="white",bg="black",relief=RIDGE,bd=10)
labelnote.place(x=720,y=400)
notevalue=Entry(window,font=("Arial",20),fg="white",bg="red",bd=10,relief=RIDGE)
notevalue.insert(0,new[10])
notevalue.place(x=1110,y=400)

submit_button=Button(window,text="Submit",command=scoreboard,font=("Arial",30,"bold"),bd=20,relief=RIDGE)
submit_button.pack(side=BOTTOM)















Leave a comment