Advertisement
noctual

Hockey scoreboard Client

Apr 12th, 2021
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.85 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import threading, socket, json, os, time
  4. from tkinter import *
  5.  
  6. SERVER_ADDR = '127.0.0.1', 1111  # Данные сервера
  7.  
  8. class HockeyScoreboard(Tk):
  9.     def __init__(self, server_adr):
  10.         super().__init__()
  11.         self.stop = True
  12.         self.timerTime = 0
  13.         self.server_adr = server_adr
  14.         self._chat_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  15.         self._chat_sock.connect(self.server_adr)
  16.         self._initUI()
  17.         rcv = threading.Thread(target=self._loop_recv)
  18.         rcv.start()
  19.         self.update()
  20.         self.mainloop()
  21.  
  22.     def _initUI(self):
  23.         self.title("Хоккейное табло")
  24.         self.configure(background='#1b191a')
  25.         self.resizable(width=False, height=False)
  26.  
  27.         self.frameScoreboard = Frame(self, background="#1b191a")
  28.         self.frameScoreboard.pack(fill=BOTH, padx=10, pady=(10, 10))
  29.  
  30.         # home team entry field
  31.         self.sv_team1_name = StringVar(value="Команда 1")
  32.         Label(self.frameScoreboard, textvariable=self.sv_team1_name, font=('Calibri', 16), fg="#ad0304", bg="black", width=10, bd=0).grid(row=0, column=0, columnspan=2)
  33.  
  34.         # guest team entry field
  35.         self.sv_team2_name = StringVar(value="Команда 2")
  36.         Label(self.frameScoreboard, textvariable=self.sv_team2_name, font=('Calibri', 16), fg="#ad0304", bg="black", width=10, bd=0, justify=RIGHT).grid(row=0, column=2, columnspan=2)
  37.        
  38.         # labels
  39.         Label(self.frameScoreboard, text="Счет", fg="white", bg="#1b191a").grid(row=1, column=0)
  40.         Label(self.frameScoreboard, text="Счет", fg="white", bg="#1b191a").grid(row=1, column=3)
  41.  
  42.         # timer
  43.         self.frameTime = Frame(self.frameScoreboard, background="#1b191a")
  44.         self.frameTime.grid(row=1, column=1, rowspan=2, columnspan=2)
  45.  
  46.         Label(self.frameTime, text="Время", fg="white", bg="#1b191a").pack(side=TOP, fill=X)
  47.  
  48.         self.sv_min = StringVar(value="00")
  49.         Label(self.frameTime, textvariable=self.sv_min, font=('Calibri', 16), foreground="#ad0304", background="black", width=2, bd=0, justify=RIGHT).pack(side=LEFT, fill=Y)
  50.  
  51.         Label(self.frameTime, text=":", font=('Calibri', 16), fg="#ad0304", bg="black").pack(side=LEFT)
  52.  
  53.         self.sv_sec = StringVar(value="00")
  54.         Label(self.frameTime, textvariable=self.sv_sec, font=('Calibri', 16), foreground="#ad0304", background="black", width=2, bd=0).pack(side=LEFT, fill=Y)
  55.        
  56.         # score labels
  57.         self.sv_team1_score = StringVar(value="00")
  58.         Label(self.frameScoreboard, textvariable=self.sv_team1_score, font=('Calibri', 30), fg="#ad0304", bg="black").grid(row=2, column=0)
  59.  
  60.         self.sv_team2_score = StringVar(value="00")
  61.         Label(self.frameScoreboard, textvariable=self.sv_team2_score, font=('Calibri', 30), fg="#ad0304", bg="black").grid(row=2, column=3)
  62.  
  63.     def _loop_recv(self):
  64.         request = b''
  65.         while True:
  66.             try:
  67.                 indexEndHead = request.find(b'\n')
  68.                 if indexEndHead != -1:            
  69.                     rec = request[:indexEndHead]
  70.                     datajs = json.loads(rec.decode())
  71.                     self.set(datajs)
  72.                     request = request[indexEndHead+1:]
  73.                 else:
  74.                     request += self._chat_sock.recv(1024)
  75.             except:
  76.                 self._chat_sock.close()
  77.                 break
  78.  
  79.     def update(self):
  80.         if not self.stop:
  81.             deltaTime = self.timerTime - (time.time() - self.startTime)
  82.             if deltaTime > 0:
  83.                 self.setTime(deltaTime)
  84.             else:
  85.                 self.stop = True
  86.                 self.setTime(0)
  87.         self.after(999, self.update)
  88.  
  89.     def setTime(self, timeInSec):
  90.         sec, min = int(timeInSec%60), int(timeInSec//60)
  91.         self.sv_min.set(f'{min:02d}')
  92.         self.sv_sec.set(f'{sec:02d}')
  93.  
  94.     def set(self, data):
  95.         if 'score' in data:
  96.             if 'team_1' in data['score']:
  97.                 self.sv_team1_score.set(f"{data['score']['team_1']:02.0f}")
  98.             if 'team_2' in data['score']:
  99.                 self.sv_team2_score.set(f"{data['score']['team_2']:02.0f}")
  100.        
  101.         if 'names' in data:
  102.             if 'team_1' in data['names']:
  103.                 self.sv_team1_name.set(str(data['names']['team_1']))
  104.             if 'team_2' in data['names']:
  105.                 self.sv_team2_name.set(str(data['names']['team_2']))
  106.  
  107.         if 'timer' in data:
  108.             if data['timer']['work']:
  109.                 self.timerTime = data['timer']['sec']
  110.                 self.startTime = data['timer']['start']
  111.                 self.setTime(data['timer']['sec'])
  112.             self.stop = not data['timer']['work']
  113.  
  114.  
  115. def main():
  116.     HockeyScoreboard(SERVER_ADDR)
  117.  
  118. if __name__ == '__main__':
  119.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement