📜  使用Python的实时板球记分牌的 GUI 应用程序

📅  最后修改于: 2022-05-13 01:55:14.018000             🧑  作者: Mango

使用Python的实时板球记分牌的 GUI 应用程序

在本文中,我们将看到如何导入和实施sports.py 模块以生成指定运动(如棒球、篮球、板球等)的记分牌以及有关该运动的其他详细信息。下面文本中的代码示例围绕板球展开,您可以对任何其他运动执行相同的操作。

Sport.py 数据来自:

  • scorepro.com
  • pro-football-reference.com
  • 棒球-reference.com
  • 篮球-reference.com
  • hockey-reference.com

此模块并非支持所有运动,下面列出了所有支持的运动以及它们各自的Python代码以供参考:

SPORT

PYTHON REFERENCE

Baseballsports.BASEBALL
Basketballsports.BASKETBALL
Cricketsports.CRICKET
Handballsports.HANDBALL
Footballsports.FOOTBALL
Hockey sports.HOCKEY
Rugby Union sports.RUGBY_U
Rugby Leaguesports.RUGBY_L
Soccersports.SOCCER
Tennissports.TENNIS
Volleyballsports.VOLLEYBALL

安装

首先,我们需要安装这个模块,为此只需在终端中运行以下代码:

pip install sports.py

执行

  • 导入模块
  • 使用 get_match() 获取单个匹配项

句法-

get_match(sport, team1, team2)

get_match() 返回一个包含以下属性的 Match 对象:

PROPERTY

DESCRIPTION

sportName of the sport
leagueLeague of the match
home_teamHome team
away_teamAway team
home_scoreHome team score
away_scoreAway team score
match_timeCurrent match time
match_dateDate on which the match was played
match_linkLink to an XML file containing match data

示例 1:

Python3
# import module
import sports
  
# setting sport
sports.get_match(sports.CRICKET, 'KINGS XI PUNJAB' , 'ROYAL CHALLENGERS BANGALORE')


Python3
# import module
import sports
  
# extracting information
matches = sports.get_sport(sports.CRICKET)
  
#printing all matches 
for item in matches:
    print(item)


Python3
# import modules
from tkinter import *
import sports
from tkinter import messagebox 
  
def cricket_info():
      
    try:
        match = sports.get_match(sports.CRICKET, e1.get() , e2.get())
        date.set(match.match_date)
        time.set(match.match_time)
        league.set(match.league)
        team1.set(match.home_team)
        team2.set(match.away_team)
        team1_score.set(match.away_score)
        team2_score.set(match.home_score)
        link.set(match.match_link)
    except:
        messagebox.showerror("showerror", "No match found") 
  
  
  
  
# object of tkinter
# and background set for light grey
master = Tk()
master.configure(bg='light grey')
  
# Variable Classes in tkinter
date = StringVar();
time = StringVar();
league = StringVar();
team1 = StringVar();
team2 = StringVar();
team1_score = StringVar();
team2_score = StringVar();
link = StringVar();
  
# Creating label for each information
# name using widget Label 
Label(master, text="Team 1 :" , bg = "light grey").grid(row=0, sticky=W)
Label(master, text="Team 2 :" , bg = "light grey").grid(row=1, sticky=W)
Label(master, text="Date :" , bg = "light grey").grid(row=2, sticky=W)
Label(master, text="Time :", bg = "light grey").grid(row=3, sticky=W)
Label(master, text="League :", bg = "light grey").grid(row=4, sticky=W)
Label(master, text="Team 1 :", bg = "light grey").grid(row=5, sticky=W)
Label(master, text="Team 2 :", bg = "light grey").grid(row=6, sticky=W)
Label(master, text="Team 1 score :", bg = "light grey").grid(row=7, sticky=W)
Label(master, text="Team 2 score :", bg = "light grey").grid(row=8, sticky=W)
Label(master, text="Link :", bg = "light grey").grid(row=9, sticky=W)
  
  
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable= date ,bg = "light grey").grid(row=2,column=1, sticky=W)
Label(master, text="", textvariable= time ,bg = "light grey").grid(row=3,column=1, sticky=W)
Label(master, text="", textvariable= league ,bg = "light grey").grid(row=4,column=1, sticky=W)
Label(master, text="", textvariable= team1 ,bg = "light grey").grid(row=5,column=1, sticky=W)
Label(master, text="", textvariable= team2 ,bg = "light grey").grid(row=6,column=1, sticky=W)
Label(master, text="", textvariable= team1_score ,bg = "light grey").grid(row=7,column=1, sticky=W)
Label(master, text="", textvariable= team2_score ,bg = "light grey").grid(row=8,column=1, sticky=W)
Label(master, text="", textvariable= link ,bg = "light grey").grid(row=9,column=1, sticky=W)
  
  
e1 = Entry(master)
e1.grid(row=0, column=1)
  
e2 = Entry(master)
e2.grid(row=1, column=1)
  
# creating a button using the widget  
# Button that will call the submit function 
b = Button(master, text="Show", command=cricket_info )
b.grid(row=0, column=2,columnspan=2, rowspan=2,padx=5, pady=5)
  
mainloop()


输出:

示例 2:

打印所有现场板球比赛的程序。

蟒蛇3

# import module
import sports
  
# extracting information
matches = sports.get_sport(sports.CRICKET)
  
#printing all matches 
for item in matches:
    print(item)

输出:

示例 3:

使用 tkinter 以 GUI 格式生成板球实时比分的应用程序。

蟒蛇3

# import modules
from tkinter import *
import sports
from tkinter import messagebox 
  
def cricket_info():
      
    try:
        match = sports.get_match(sports.CRICKET, e1.get() , e2.get())
        date.set(match.match_date)
        time.set(match.match_time)
        league.set(match.league)
        team1.set(match.home_team)
        team2.set(match.away_team)
        team1_score.set(match.away_score)
        team2_score.set(match.home_score)
        link.set(match.match_link)
    except:
        messagebox.showerror("showerror", "No match found") 
  
  
  
  
# object of tkinter
# and background set for light grey
master = Tk()
master.configure(bg='light grey')
  
# Variable Classes in tkinter
date = StringVar();
time = StringVar();
league = StringVar();
team1 = StringVar();
team2 = StringVar();
team1_score = StringVar();
team2_score = StringVar();
link = StringVar();
  
# Creating label for each information
# name using widget Label 
Label(master, text="Team 1 :" , bg = "light grey").grid(row=0, sticky=W)
Label(master, text="Team 2 :" , bg = "light grey").grid(row=1, sticky=W)
Label(master, text="Date :" , bg = "light grey").grid(row=2, sticky=W)
Label(master, text="Time :", bg = "light grey").grid(row=3, sticky=W)
Label(master, text="League :", bg = "light grey").grid(row=4, sticky=W)
Label(master, text="Team 1 :", bg = "light grey").grid(row=5, sticky=W)
Label(master, text="Team 2 :", bg = "light grey").grid(row=6, sticky=W)
Label(master, text="Team 1 score :", bg = "light grey").grid(row=7, sticky=W)
Label(master, text="Team 2 score :", bg = "light grey").grid(row=8, sticky=W)
Label(master, text="Link :", bg = "light grey").grid(row=9, sticky=W)
  
  
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable= date ,bg = "light grey").grid(row=2,column=1, sticky=W)
Label(master, text="", textvariable= time ,bg = "light grey").grid(row=3,column=1, sticky=W)
Label(master, text="", textvariable= league ,bg = "light grey").grid(row=4,column=1, sticky=W)
Label(master, text="", textvariable= team1 ,bg = "light grey").grid(row=5,column=1, sticky=W)
Label(master, text="", textvariable= team2 ,bg = "light grey").grid(row=6,column=1, sticky=W)
Label(master, text="", textvariable= team1_score ,bg = "light grey").grid(row=7,column=1, sticky=W)
Label(master, text="", textvariable= team2_score ,bg = "light grey").grid(row=8,column=1, sticky=W)
Label(master, text="", textvariable= link ,bg = "light grey").grid(row=9,column=1, sticky=W)
  
  
e1 = Entry(master)
e1.grid(row=0, column=1)
  
e2 = Entry(master)
e2.grid(row=1, column=1)
  
# creating a button using the widget  
# Button that will call the submit function 
b = Button(master, text="Show", command=cricket_info )
b.grid(row=0, column=2,columnspan=2, rowspan=2,padx=5, pady=5)
  
mainloop()

输出: