如何在python中使用tkinter构建Othello GUI?具体来说,我如何开始使最初的四个部分出现?当我选择一个正方形时,我如何让我的板打印出一块作品的位置?到目前为止,它打印出"189.0,126.0,252.0,189.0“当点击一块。我真的只是寻求指导,任何帮助都是非常感谢的!这是我到目前为止的代码。
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):
for c in range(self._columns):
for 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
for x in range(self._columns):
for 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()发布于 2015-06-03 20:26:16
使用canvas.create_oval(bbox, **options)绘制光盘。
使用标记来区分画布项目:
标记是附加在项目上的符号名称。标记是普通字符串,它们可以包含除空格以外的任何内容。
我建议您标记每个单元格的每个元素(矩形和椭圆形),并使用一个标记,使您能够识别它。
item = canvas.create_oval(x1, x2, y1, y2, tags=("x=1","y=3"))单击项目时,您可以使用
canvas.gettags(item)然后迭代它的所有标记:如果标记以"x="或"y="开头,那么它包含行/列信息,您可以使用int(tagname[2:])提取这些信息
https://stackoverflow.com/questions/30629967
复制相似问题