Building Othello game using Tkinter (Python) -
how go building othello gui using tkinter in python? how begin making initial 4 pieces show up? how board print out position of piece when select square? far prints out "[189.0, 126.0, 252.0, 189.0]" when click on piece. looking guidance, appreciated! here code have far.
import tkinter class ra: def __init__(self): self._columns = 8 self._rows = 8 self._root = tkinter.tk() self._canvas = tkinter.canvas(master = self._root, height = 500, width = 500, background = 'green') self._canvas.pack(fill = tkinter.both, expand = true) self._canvas.bind('<configure>',self.draw_handler) def run(self): self._root.mainloop() def draw(self): c in range(self._columns): r in range(self._rows): x1 = c * (column_width) y1 = r * (row_height) x2 = x1 + (column_width) y2 = y1 + (row_height) def clicked(self,event: tkinter.event): x = event.x y = event.y coordinates = self._canvas.coords("current") print(coordinates) def draw(self): self._canvas.delete(tkinter.all) column_width = self._canvas.winfo_width()/self._columns row_height = self._canvas.winfo_height()/self._rows x in range(self._columns): y in range(self._rows): x1 = x * column_width y1 = y * row_height x2 = x1 + column_width y2 = y1 + row_height r = self._canvas.create_rectangle(x1,y1,x2,y2,fill = 'blue') self._canvas.tag_bind(r,'<buttonpress-1>',self.clicked) self._canvas.create_rectangle(x1,y1,x2,y2) self._canvas.bind('<configure>',self.draw_handler) def draw_handler(self,event): self.draw() r = ra() r.run()
draw discs using canvas.create_oval(bbox, **options).
use tags distinguish canvas items:
tags symbolic names attached items. tags ordinary strings, , can contain except whitespace.
i'd suggest tag each element (rectangle , oval) of each cell, tag allow recognize it.
item = canvas.create_oval(x1, x2, y1, y2, tags=("x=1","y=3")) when item clicked, can of tags with
canvas.gettags(item) then iterate of tags: if tag starts "x=" or "y=", contains row/column information, can extract int(tagname[2:])
Comments
Post a Comment