如果您不需要移动瓷砖的动画,那么您可以使用grid() 将标签放在行和列中 - 而不是使用Canvas - 并将文本从标签移动到标签。
您甚至可以使用Button(而不是Label)和command= 为按钮分配功能。
-
如果您需要动画,则可以使用create_window() 将Label 添加到Canvas 和bind(event, function) 以将函数分配给事件调用的标签(如)。
但您也可以使用place() 在Frame 中放置标签或按钮
-
并且您必须将所有标签/按钮保留在全局列表中才能访问它们。
-
编辑:简单且有效的示例(但不完整)
import tkinter as tk
# --- functions ---
def move(x, y):
global empty_x, empty_y, game_running
if game_running:
# check and move tile
if (empty_x, empty_y) in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:
board[empty_y][empty_x] = board[y][x]
board[y][x] = ' '
buttons[empty_y][empty_x]['text'] = buttons[y][x]['text']
buttons[y][x]['text'] = ' '
empty_x = x
empty_y = y
if won():
# show label "Hurray"
label.lift()
game_running = False
else:
# start game again
shuffle() # shuffle tiles
game_running = True
# hide label
label.lower() # or frame.lift()
def won():
number = 0
for y, row in enumerate(board):
for x, char in enumerate(row, 1):
number += 1
if number == 9 and char == ' ':
#print('A |%s| %d' % (char, number))
return True
elif char != str(number):
#print('B |%s| %d' % (char, number))
return False
return True
def shuffle():
# TODO: shuffle tiles
# it can't put tiles in random places
# because it can create game without solution
pass
# --- data ---
board = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', ' '],
]
empty_x = len(board[0])-1
empty_y = len(board)-1
# --- main ---
root = tk.Tk()
# create label hidden behide frame
label = tk.Label(root, text="Hurray!\n\n(click any button to play again)", bg="red")
label.grid(row=0, column=0, ipadx=20, ipady=20)
# create frame with buttons to fast show/hide label
frame = tk.Frame(root)
frame.grid(row=0, column=0) # the same (row,col) to hide label
buttons = []
for y, row in enumerate(board):
buttons_row = []
for x, char in enumerate(row):
b = tk.Button(frame, text=char, width=10, height=5, command=lambda x=x,y=y:move(x,y))
b.grid(row=y, column=x)
buttons_row.append(b)
buttons.append(buttons_row)
# start game
shuffle() # shuffle tiles
game_running = True
# start "the engine"
root.mainloop()
编辑:带有StringVar的新版本
在shuffle 中,我将图块放在随机位置,有时我会得到没有解决方案的游戏。
有一些较小的修改 - “Hurray”现在是一个按钮并开始新游戏。
import tkinter as tk
import random
# --- functions ---
def move(x, y):
global empty_x, empty_y, game_running
if game_running:
# check and move tile
if (empty_x, empty_y) in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:
board[empty_y][empty_x].set( board[y][x].get() )
board[y][x].set(' ')
empty_x = x
empty_y = y
if won():
# show label "Hurray"
label.lift()
game_running = False
else:
# start game again
shuffle() # shuffle tiles
def won():
number = 0
for y, row in enumerate(board):
for x, string_var in enumerate(row, 1):
number += 1
if number == 9 and string_var.get() == ' ':
#print('A |%s| %d' % (char, number))
return True
elif string_var.get() != str(number):
#print('B |%s| %d' % (char, number))
return False
return True
def shuffle():
global empty_x, empty_y, game_running
# TODO: shuffle tiles
# it can't put tiles in random places
# because it can create game without solution
# clear all tiles to recognize (in next step) empty tiles
for row in board:
for element in row:
element.set('')
# put tiles in random places
for number in range(1, 10):
while True:
row = random.randint(0, 2)
col = random.randint(0, 2)
if board[row][col].get() == '':
board[row][col].set(str(number))
print('row/col/number:', row, col, number)
break
# last number remove:
print('empty row/col:', row, col)
board[row][col].set(' ')
empty_x = col
empty_y = row
# hide label
label.lower() # or frame.lift()
# start game
game_running = True
# --- main ---
root = tk.Tk()
# --- data ---
# StringVar needs `root` - it has to be after tk.Tk()
# empty board
board = [
# [tk.StringVar(value='1'), tk.StringVar(value='2'), tk.StringVar(value='3')],
[tk.StringVar(), tk.StringVar(), tk.StringVar()],
[tk.StringVar(), tk.StringVar(), tk.StringVar()],
[tk.StringVar(), tk.StringVar(), tk.StringVar()],
]
# create label hidden behide frame
label = tk.Button(root, text="Hurray!\n\n(click here to play again)", command=shuffle)
label.grid(row=0, column=0, ipadx=50, ipady=50)
# create frame with buttons to fast show/hide label
frame = tk.Frame(root)
frame.grid(row=0, column=0) # the same (row,col) to hide label
buttons = []
for y, row in enumerate(board):
buttons_row = []
for x, string_var in enumerate(row):
b = tk.Button(frame, textvariable=string_var, width=10, height=5, command=lambda x=x,y=y:move(x,y))
b.grid(row=y, column=x)
buttons_row.append(b)
buttons.append(buttons_row)
# start game
shuffle() # shuffle tiles
# start "the engine"
root.mainloop()