import tkinter from tkinter import filedialog import os class Terrain: def __init__(self): self.height = 15000//25 self.width = 28000//25 self.three_points = 6250//25 # rayon self.middle_circle = 1800//25 # rayon class Team: def __init__(self): self.players = ['arthur', 'jl', 'léandre', "louis", "alex"] self.filemenu = 'stat.txt' self.posx, self.posy = 0,0 # coo de la dernière action self.ac_player = 0 # dernier joueur class Gui: def __init__(self, ): self.terrain = Terrain() self.team = Team() self.file = 'untitled.bbm' self.temp_data = '' self.list_button_menu = [] self.root = tkinter.Tk() self.root.title(self.file) self.canvas = tkinter.Canvas(self.root, width=self.terrain.width, height=self.terrain.height, background='#bbb') self.canvas.pack() # topbar self.menubar = tkinter.Menu(self.root) self.filemenu = tkinter.Menu(self.menubar, tearoff=0) self.filemenu.add_command(label="new", command=self.new) self.filemenu.add_command(label="open", command=self.browse_open) self.filemenu.add_command(label="save", command=self.save) self.filemenu.add_separator() self.filemenu.add_command(label="rename") self.filemenu.add_command(label="exit", command=self.root.quit) self.menubar.add_cascade(label="file", menu=self.filemenu) self.root.config(menu=self.menubar) # menu contextuel des actions self.action = tkinter.Menu(self.root, tearoff=0) self.action.add_command(label='marque', command=lambda: self.action_menu('marque')) self.action.add_command(label='rate', command=lambda: self.action_menu('rate')) # Menu contextuel des joueurs self.menu = tkinter.Menu(self.root, tearoff = 0) self.menu.add_command(label=self.team.players[0], command=lambda: self.show_sub_menu(self.team.players[0])) self.menu.add_command(label=self.team.players[1], command=lambda: self.show_sub_menu(self.team.players[1])) self.menu.add_command(label=self.team.players[2], command=lambda: self.show_sub_menu(self.team.players[2])) self.menu.add_command(label=self.team.players[3], command=lambda: self.show_sub_menu(self.team.players[3])) self.menu.add_command(label=self.team.players[4], command=lambda: self.show_sub_menu(self.team.players[4])) def new(self, *event): # créer un fichier vierge self.file = 'untitled.bbm' self.root.title = self.file def save(self, *event): # sauvegarde du fichier if self.file == 'untitled.bbm' and self.file == '': self.file = tkinter.filedialog.asksaveasfilename(initialdir = "~", title = "Select a File", filetypes = (("BasketBall Match", "*.bbm"), ("all files", "*.*"))) with open(self.file, 'a') as f: f.write(self.temp_data) self.temp_data = '' self.root.title(self.file) def browse_open(self, *event): # ouvre un fichier self.file = tkinter.filedialog.askopenfilename(initialdir = "~/Documents/python/basket", title = "Select a File", filetypes = (("BasketBall Match", "*.bbm"), ("all files", "*.*"))) with open(self.file, 'r') as f: f.read() self.root.title(self.file) def context_menu(self, event): self.team.posx, self.team.posy = event.x, event.y self.coo = [event.x_root, event.y_root] self.menu.tk_popup(event.x_root, event.y_root) self.menu.grab_release() def show_sub_menu(self, player): self.team.ac_player = player self.action.tk_popup(self.coo[0], self.coo[1]) self.action.grab_release() def action_menu(self, action): # test si l'action est dans la liste des 3 points if ((self.team.posx-0)**2) + ((self.team.posy-self.terrain.height/2)**2) <= self.terrain.three_points**2 or ((self.team.posx-self.terrain.width)**2) + ((self.team.posy-self.terrain.height/2)**2) <= self.terrain.three_points**2: score = 2 else: score = 3 if action == 'marque': self.canvas.create_oval(self.team.posx-12, self.team.posy-12, self.team.posx+12, self.team.posy+12, fill='green') else: self.canvas.create_oval(self.team.posx-12, self.team.posy-12, self.team.posx+12, self.team.posy+12, fill='red') if self.temp_data == '': self.root.title(self.file+' *') self.temp_data += self.team.ac_player+' '+action+' '+str(score)+' '+str(self.team.posx)+' '+str(self.team.posy)+'\n' def draw_actions(self, *event): # dessine l'entièreté des actions items = self.canvas.find_all() while len(items) > 4: self.canvas.delete(items[-1]) items = self.canvas.find_all() def draw_terrain(self): # ligne des 3 points self.canvas.create_arc(-self.terrain.three_points, self.terrain.height//2 - self.terrain.three_points, self.terrain.three_points, self.terrain.height - (self.terrain.height//2 - self.terrain.three_points),width=3, start=90, extent=-180) self.canvas.create_arc(self.terrain.width-self.terrain.three_points, self.terrain.height//2 - self.terrain.three_points, self.terrain.width+self.terrain.three_points, self.terrain.height - (self.terrain.height//2 - self.terrain.three_points),width=3, start=90, extent=180) # ligne centrale self.canvas.create_line(self.terrain.width//2, 0, self.terrain.width//2, self.terrain.height, width=3) # cercle du milieu self.canvas.create_oval(self.terrain.width//2 - self.terrain.middle_circle, self.terrain.height//2 - self.terrain.middle_circle, self.terrain.width//2 + self.terrain.middle_circle, self.terrain.height//2 + self.terrain.middle_circle, width=3) gui = Gui() gui.draw_terrain() gui.root.bind("", gui.context_menu) gui.root.bind("", gui.save) gui.root.bind("", gui.browse_open) gui.root.bind("", gui.new) gui.root.bind("", gui.draw_actions) gui.root.mainloop()