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 *
- LISTEN_PORT = 1111
- COUNT_MAX_PEER = 5
- class Peer(object):
- def __init__(self, server, sock):
- self._sock = sock
- self._server = server
- def send(self, data):
- try:
- return self._sock.send(data)
- except IOError:
- self._server.remove(self)
- class HockeyScoreboardServer(Tk):
- def __init__(self, port, max_peer):
- super().__init__()
- # init timer
- self.stop = True
- self.timerTime = 0
- self.startTime = 0
- #init server
- self._serv_sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
- #self._serv_sock.setblocking(0)
- self._serv_sock.bind(('', port))
- self._serv_sock.listen(max_peer)
- self._peers = []
- rcv = threading.Thread(target=self._server)
- rcv.start()
- #tk
- self.initUI()
- self.update()
- self.mainloop()
- # Server
- def _compress(self, data):
- return (json.dumps(data)+'\n').encode('utf8')
- def remove(self, peer):
- self._peers.remove(peer)
- def sendAllPeers(self, data):
- message = self._compress(data)
- for peer in self._peers:
- peer.send(message)
- def _server(self):
- while True:
- peer_sock, peer_name = self._serv_sock.accept()
- peer = Peer(self, peer_sock)
- self._peers.append(peer)
- peer.send(self._compress({
- 'names': {
- 'team_1': self.sv_team1_name.get(),
- 'team_2': self.sv_team2_name.get()
- },
- 'score': {
- 'team_1': self.score1.get(),
- 'team_2': self.score2.get()
- },
- 'timer': {
- 'sec': self.timerTime,
- 'start': self.startTime,
- 'work': not self.stop
- }
- }))
- # Window
- 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")
- self.entry1 = Entry(self.frameScoreboard, textvariable=self.sv_team1_name, font=('Calibri', 16), fg="#ad0304", bg="black", width=10, bd=0)
- self.sv_team1_name.trace("w", lambda name, index, mode, sv=self.sv_team1_name: self.controlStr(self.entry1, 10, "team_1"))
- self.entry1.grid(row=0, column=0, columnspan=2)
- # guest team entry field
- self.sv_team2_name = StringVar(value="Команда 2")
- self.entry2 = Entry(self.frameScoreboard, textvariable=self.sv_team2_name, font=('Calibri', 16), fg="#ad0304", bg="black", width=10, bd=0, justify=RIGHT)
- self.sv_team2_name.trace("w", lambda name, index, mode, sv=self.sv_team2_name: self.controlStr(self.entry2, 10, "team_2"))
- self.entry2.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)
- self.labelTimeTitle = Label(self.frameTime, text="Время", fg="white", bg="#1b191a")
- self.labelTimeTitle.pack(side=TOP, fill=X)
- self.svMin = StringVar(value="00")
- self.entryMin = Entry(self.frameTime, textvariable=self.svMin, font=('Calibri', 16), foreground="#ad0304", disabledforeground="#ad0304", background="black", disabledbackground="black", width=2, bd=0, justify=RIGHT)
- self.svMin.trace("w", lambda name, index, mode, sv=self.svMin: self.controlStr(self.entryMin, 2, None))
- self.entryMin.pack(side=LEFT, fill=Y)
- Label(self.frameTime, text=":", font=('Calibri', 16), fg="#ad0304", bg="black").pack(side=LEFT)
- self.svSec = StringVar(value="00")
- self.entrySec = Entry(self.frameTime, textvariable=self.svSec, font=('Calibri', 16), foreground="#ad0304", disabledforeground="#ad0304", background="black", disabledbackground="black", width=2, bd=0)
- self.svSec.trace("w", lambda name, index, mode, sv=self.svSec: self.controlStr(self.entrySec, 2, None))
- self.entrySec.pack(side=LEFT, fill=Y)
- # score labels
- self.labelScore1 = Label(self.frameScoreboard, text="00", font=('Calibri', 30), fg="#ad0304", bg="black")
- self.labelScore1.grid(row=2, column=0)
- self.labelScore2 = Label(self.frameScoreboard, text="00", font=('Calibri', 30), fg="#ad0304", bg="black")
- self.labelScore2.grid(row=2, column=3)
- # managing score home team
- self.frameManagement1 = Frame(self.frameScoreboard, background="#1b191a")
- self.frameManagement1.grid(row=3, column=0)
- self.score1 = self.score(self, self.labelScore1, "team_1")
- Button(self.frameManagement1, text="+",
- background="#1b191a", activebackground="#ad0304",
- foreground="#ad0304", activeforeground="#1b191a",
- padx="3", command=self.score1.Increase).grid(row=0, column=0, pady=5)
- Button(self.frameManagement1, text="-",
- background="#1b191a", activebackground="#ad0304",
- foreground="#ad0304", activeforeground="#1b191a",
- padx="3", command=self.score1.Decrease).grid(row=0, column=1, pady=5)
- # managing timer
- self.frameManagementTime = Frame(self.frameScoreboard, background="#1b191a")
- self.frameManagementTime.grid(row=3, column=1, columnspan=2)
- self.buttonTextStartStop = StringVar(value="Старт")
- Button(self.frameManagementTime, textvariable=self.buttonTextStartStop,
- background="#1b191a", activebackground="#ad0304",
- foreground="#ad0304", activeforeground="#1b191a",
- padx="3", command=self.startstopTimer
- ).grid(row=0, column=0, ipady=5)
- # managing score guest team
- self.frameManagement2 = Frame(self.frameScoreboard, background="#1b191a")
- self.frameManagement2.grid(row=3, column=3)
- self.score2 = self.score(self, self.labelScore2, "team_2")
- Button(self.frameManagement2, text="+",
- background="#1b191a", activebackground="#ad0304",
- foreground="#ad0304", activeforeground="#1b191a",
- padx="3", command=self.score2.Increase).grid(row=0, column=0, pady=5)
- Button(self.frameManagement2, text="-",
- background="#1b191a", activebackground="#ad0304",
- foreground="#ad0304", activeforeground="#1b191a",
- padx="3", command=self.score2.Decrease).grid(row=0, column=1, pady=5)
- def update(self):
- if not self.stop:
- deltaTime = self.timerTime - (time.time() - self.startTime)
- if deltaTime > 0:
- self.setTime(deltaTime)
- else:
- self.startstopTimer()
- self.setTime(0)
- self.after(1000, self.update)
- def startstopTimer(self):
- if self.stop:
- tempTime = self.getTime()
- if tempTime != None and tempTime > 0:
- self.timerTime = tempTime
- self.buttonTextStartStop.set("Стоп")
- self.entryMin.config(state='disabled')
- self.entrySec.config(state='disabled')
- self.startTime = time.time()
- else:
- self.timerTime = 0
- return
- else:
- self.buttonTextStartStop.set("Старт")
- self.entryMin.config(state='normal')
- self.entrySec.config(state='normal')
- self.timerTime -= (time.time() - self.startTime)
- self.setTime(self.timerTime)
- self.stop = not self.stop
- self.sendAllPeers({
- 'timer': {
- 'sec': self.timerTime,
- 'start': self.startTime,
- 'work': not self.stop
- }
- })
- def setTime(self, timeInSec):
- self.svMin.set(f'{(timeInSec//60):02.0f}')
- self.svSec.set(f'{(timeInSec%60):02.0f}')
- def getTime(self):
- minutes, seconds = self.svMin.get(), self.svSec.get()
- if minutes.isdigit() and seconds.isdigit():
- minutes, seconds = int(minutes), int(seconds)
- if minutes >= 0 and minutes <= 99 and seconds >= 0 and seconds <= 59:
- return minutes * 60 + seconds
- def controlStr(self, entry, max, team):
- text = entry.get()
- if len(text) > max:
- entry.delete(max,END)
- if team != None:
- self.sendAllPeers({'names': {team: entry.get()}})
- class score:
- def __init__(self, server, label, team):
- self.server = server
- self.label = label
- self.team = team
- self.scope = 0
- def Increase(self):
- if self.scope < 99:
- self.scope += 1
- self.__update()
- def Decrease(self):
- if self.scope > 0:
- self.scope -= 1
- self.__update()
- def get(self):
- return self.scope
- def __update(self):
- self.label.configure(text=f'{self.scope:02.0f}')
- self.server.sendAllPeers({'score':{self.team: self.scope}})
- def main():
- HockeyScoreboardServer(LISTEN_PORT, COUNT_MAX_PEER)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement