【文件属性】:
文件名称:用tkinter 实现的五子棋UI界面
文件大小:5KB
文件格式:PY
更新时间:2022-06-26 14:40:43
gobang
用tkinter 实现的五子棋UI界面
import tkinter as tk
from tkinter import messagebox
from chessboard import ChessBoard
ChessBoard = ChessBoard()
class GUI(object):
def __init__(self):
self.counter = 0
self.winner = 0
self.is_start = False
self.is_surrender = False
self.window = tk.Tk() # 窗口对象(首字母大写)
self.window.title('Gobang')
self.window.geometry('800x540')
self.window.resizable(width = False, height = False)
# 画布对象 棋盘
self.canvas = tk.Canvas(self.window, height = 540, width = 540)
self.chessboard = tk.PhotoImage(file = 'Gobang_chessboard/chessboard.gif')
self.blackpoint = tk.PhotoImage(file = 'Gobang_chessboard/blackpoint.gif')
self.whitepoint = tk.PhotoImage(file = 'Gobang_chessboard/whitepoint.gif')
self.canvas.create_image(0, 0, anchor = 'nw', image = self.chessboard)
self.canvas.bind("
", self.get_point)
self.start_point = 10 # 起始点位置
self.step = 35 # 每个格子的跨度
self.canvas.place(x = 0, y = 0)
# 标签对象
self.l_info = tk.Label(self.window, text = 'Not started', font=('Arial', 12), width = 25, height = 2)
self.l_info.place(x = 545, y = 0)
# 文本框对象
self.t = tk.Text(self.window, height = 15)
self.t.place(x = 540, y = 40)
# 按钮对象
self.f_header = tk.Frame(self.window, highlightthickness=0)
self.b_start = tk.Button(self.f_header, text = 'start', command = self.start)
self.b_restart = tk.Button(self.f_header, text = 'restart', command = self.restart)
self.b_regret = tk.Button(self.f_header, text = 'regret', command = self.regret)
self.b_surrender = tk.Button(self.f_header, text = 'surrender', command = self.surrender)
self.f_header.place(x = 545, y = 250)
self.b_start.pack(side='left', padx=10)
self.b_restart.pack(side = 'left')
self.b_surrender.pack(side = 'right')
self.b_regret.pack(side = 'right', padx=10)
self.window.mainloop() # 循环显示
# 转换坐标
def get_point(self, e):
x, y = int((e.x - self.start_point) / self.step), int((e.y - self.start_point) / self.step)
print([x, y])
self.info()
if self.is_start is True and self.is_surrender is False:
self.put_chess(x, y)
# 下棋
def put_chess(self, x, y):
x_p, y_p = int((x * self.step) + self.start_point + 15), int((y * self.step) + self.start_point + 15)
if self.counter % 2 == 0:
ChessBoard.draw_xy(x, y, state = 1)
self.canvas.create_image(x_p, y_p, anchor = 'center', image = self.blackpoint)
self.winner = ChessBoard.anyone_win(x, y)
self.info()
self.counter += 1
else:
ChessBoard.draw_xy(x, y, state = 2)
self.canvas.create_image(x_p, y_p, anchor = 'center', image = self.whitepoint)
self.winner = ChessBoard.anyone_win(x, y)
self.info()
self.counter += 1
# 提示语
def info(self):
if self.is_start is True and self.is_surrender is False:
if self.counter % 2 == 1:
self.l_info.config(text = 'Turn for black')
else:
self.l_info.config(text = 'Turn for white')
if self.winner != 0:
self.is_start = False
if self.winner == 1:
self.l_info.config(text = 'black win')
else:
self.l_info.config(text = 'white win')
# 开始
def start(self):
self.is_start = True
self.l_info.config(text = 'Turn for black')
# 重来
def restart(self):
self.canvas.create_image(0, 0, anchor = 'nw', image = self.chessboard)
ChessBoard.reset()
self.counter = 0
self.winner = 0
self.is_start = True
self.is_surrender = False
self.l_info.config(text = 'Turn for black')
# 悔棋
def regret(self):
if self.counter != 0:
pass
# 认输
def surrender(self):
if self.is_start is True and self.counter != 0:
self.is_surrender = True
if self.counter % 2 == 0:
self.l_info.config(text = 'black gives up')
else:
self.l_info.config(text = 'white gives up')
if __name__ == '__main__':
GUI = GUI()
print(ChessBoard.board())