Files @ 1d4ca86c9193
Branch filter:

Location: OneEye/src/gui.py - annotation

Laman
rewritten readme
import tkinter as tk
from PIL import ImageTk
import PIL
import math
from epoint import *


class Application(tk.Frame):
  def __init__(self, master=None):
    self.corners=[]
    
    tk.Frame.__init__(self, master)
    self.grid(column=0,row=0)
    self.createWidgets()

  def createWidgets(self):
    # a captured frame with overlay graphics
    self.imgView=tk.Canvas(self)
    self.imgView.configure(width=480,height=360)
    imgOrig=PIL.Image.open("../images/1.jpg")
    self.img=ImageTk.PhotoImage(imgOrig.resize((int(self.imgView['width']),int(self.imgView['height'])),resample=PIL.Image.BILINEAR))
    
    self.imgView.bind('<1>',lambda e: self.addCorner(e.x,e.y))
    self.redrawImgView()
    
    self.imgView.grid(column=0,row=0)
    
    # board with detected stones
    self.boardView=BoardView(self)
    self.boardView.grid(column=1,row=0)
  
  ## Stores a grid corner located at x,y coordinates.
  def addCorner(self,x,y):
    a=EPoint(x,y)
    for i,c in enumerate(self.corners): # move an improperly placed point
      if a.dist(c)<20:
        self.corners[i]=a
        # print(self.corners)
        self.redrawImgView()
        return
    
    if len(self.corners)<4: # add a new corner
      self.corners.append(a)
      # print(self.corners)
      self.redrawImgView()
    
    if len(self.corners)<4: return
    
    index,minDist=0,float('inf') # replace the corner closest to the clicked point
    for i,c in enumerate(self.corners):
      if a.dist(c)<minDist:
        index,minDist=i,a.dist(c)
    
    self.corners[index]=a
    # print(self.corners)
    
    self.redrawImgView()
    
  ## Redraws the current image and its overlay.
  def redrawImgView(self):
    self.imgView.create_image(2,2,anchor="nw",image=self.img)
    for corner in self.corners:
      self.markPoint(corner.x,corner.y)
    
    self.imgView.grid()
    
  ## Marks a point at the image with a green cross. Used for corners.
  def markPoint(self,x,y):
    self.imgView.create_line(x-3,y-3,x+4,y+4,fill="#00ff00")
    self.imgView.create_line(x-3,y+3,x+4,y-4,fill="#00ff00")
    
    self.imgView.grid()


## Handles and presents the game state as detected by the program.   
class BoardView(tk.Canvas):
  def __init__(self, master=None):
    tk.Canvas.__init__(self, master)
    self.configure(width=360,height=360,background="#ffcc00")
    
    self.drawGrid()
    self.drawStones()
    
    self.grid()
    
  def drawGrid(self):
    for i in range(19):
      self.create_line(18,18*(i+1),360-18,18*(i+1),fill="#000000") # rows
      self.create_line(18*(i+1),18,18*(i+1),360-18,fill="#000000") # cols
      
    self.drawStars()
      
  def drawStars(self):
    for r in range(4,19,6):
      for c in range(4,19,6):
        self.create_oval(r*18-2,c*18-2,r*18+2,c*18+2,fill='#000000')
      
  def drawStones(self):
    pass
      
  ## Draws a stone at provided coordinates.
  #  
  #  @param r row coordinate, [0-18], counted from top
  #  @param c column coordinate, [0-18], counted from left
  #  @param color color indicator, {'b','w'}
  def drawStone(self,r,c,color):
    self.create_oval(r*18-9,c*18-9,r*18+9,r*18+9,'#000000' if color=='b' else '#ffffff')


root = tk.Tk()
app = Application(master=root)
app.mainloop()