Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import threading, socket, json, os, time
- from tkinter import *
- SERVER_ADDR = '127.0.0.1', 1111 # Данные сервера
- class HockeyScoreboard(Tk):
- def __init__(self, server_adr):
- super().__init__()
- self.stop = True
- self.timerTime = 0
- self.server_adr = server_adr
- self._chat_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- self._chat_sock.connect(self.server_adr)
- self._initUI()
- rcv = threading.Thread(target=self._loop_recv)
- rcv.start()
- self.update()
- self.mainloop()
- def _initUI(self):
- self.title("Хоккейное табло")
- self.configure(background='#1b191a')
- self.resizable(width=False, height=False)
- self.frameScoreboard = Frame(self, background="#1b191a")
- self.frameScoreboard.pack(fill=BOTH, padx=10, pady=(10, 10))
- # home team entry field
- self.sv_team1_name = StringVar(value="Команда 1")
- 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)
- # guest team entry field
- self.sv_team2_name = StringVar(value="Команда 2")
- 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)
- # labels
- Label(self.frameScoreboard, text="Счет", fg="white", bg="#1b191a").grid(row=1, column=0)
- Label(self.frameScoreboard, text="Счет", fg="white", bg="#1b191a").grid(row=1, column=3)
- # timer
- self.frameTime = Frame(self.frameScoreboard, background="#1b191a")
- self.frameTime.grid(row=1, column=1, rowspan=2, columnspan=2)
- Label(self.frameTime, text="Время", fg="white", bg="#1b191a").pack(side=TOP, fill=X)
- self.sv_min = StringVar(value="00")
- 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)
- Label(self.frameTime, text=":", font=('Calibri', 16), fg="#ad0304", bg="black").pack(side=LEFT)
- self.sv_sec = StringVar(value="00")
- Label(self.frameTime, textvariable=self.sv_sec, font=('Calibri', 16), foreground="#ad0304", background="black", width=2, bd=0).pack(side=LEFT, fill=Y)
- # score labels
- self.sv_team1_score = StringVar(value="00")
- Label(self.frameScoreboard, textvariable=self.sv_team1_score, font=('Calibri', 30), fg="#ad0304", bg="black").grid(row=2, column=0)
- self.sv_team2_score = StringVar(value="00")
- Label(self.frameScoreboard, textvariable=self.sv_team2_score, font=('Calibri', 30), fg="#ad0304", bg="black").grid(row=2, column=3)
- def _loop_recv(self):
- request = b''
- while True:
- try:
- indexEndHead = request.find(b'\n')
- if indexEndHead != -1:
- rec = request[:indexEndHead]
- datajs = json.loads(rec.decode())
- self.set(datajs)
- request = request[indexEndHead+1:]
- else:
- request += self._chat_sock.recv(1024)
- except:
- self._chat_sock.close()
- break
- def update(self):
- if not self.stop:
- deltaTime = self.timerTime - (time.time() - self.startTime)
- if deltaTime > 0:
- self.setTime(deltaTime)
- else:
- self.stop = True
- self.setTime(0)
- self.after(999, self.update)
- def setTime(self, timeInSec):
- sec, min = int(timeInSec%60), int(timeInSec//60)
- self.sv_min.set(f'{min:02d}')
- self.sv_sec.set(f'{sec:02d}')
- def set(self, data):
- if 'score' in data:
- if 'team_1' in data['score']:
- self.sv_team1_score.set(f"{data['score']['team_1']:02.0f}")
- if 'team_2' in data['score']:
- self.sv_team2_score.set(f"{data['score']['team_2']:02.0f}")
- if 'names' in data:
- if 'team_1' in data['names']:
- self.sv_team1_name.set(str(data['names']['team_1']))
- if 'team_2' in data['names']:
- self.sv_team2_name.set(str(data['names']['team_2']))
- if 'timer' in data:
- if data['timer']['work']:
- self.timerTime = data['timer']['sec']
- self.startTime = data['timer']['start']
- self.setTime(data['timer']['sec'])
- self.stop = not data['timer']['work']
- def main():
- HockeyScoreboard(SERVER_ADDR)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement