Files
@ 500e573f202d
Branch filter:
Location: OneEye/src/gui.py
500e573f202d
3.2 KiB
text/x-python
readme.md edited online with Bitbucket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | import tkinter as tk
from PIL import ImageTk
import PIL
import math
from epoint import *
from corners import *
from grid import *
class Application(tk.Frame):
def __init__(self, master=None):
self.corners=Corners()
self.boardGrid=None
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/7.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):
self.corners.add(x,y)
if self.corners.canonizeOrder():
self.boardGrid=Grid(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.corners:
self.markPoint(corner.x,corner.y)
if self.boardGrid!=None:
for r in range(19):
a=self.boardGrid.intersections[r][0]
b=self.boardGrid.intersections[r][-1]
self.imgView.create_line(a.x,a.y,b.x,b.y,fill='#00ff00')
for c in range(19):
a=self.boardGrid.intersections[0][c]
b=self.boardGrid.intersections[-1][c]
self.imgView.create_line(a.x,a.y,b.x,b.y,fill='#00ff00')
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()
|