Tkinter:从空白图块到标记 (X/O) 的图像更改不是永久性的
Tkinter: Image change from blank tile to mark (X/O) is not permanent
我正在使用 Tkinter 制作一个可扩展的井字游戏程序。当我按下一个空的方形按钮时,图像会发生变化,这是应该的。当我按下不同的按钮时,第一次按下按钮的图像完全消失,而我希望它保留下来,唯一带有 X/O 图像的按钮是最近按下的按钮。对解决这个问题有帮助吗?我是初学者。
如果您还有关于如何进行可扩展的获胜检查的任何提示(可扩展我的意思是用户可以输入板尺寸,从 2x2 到适合屏幕的大小),那将非常有帮助。
这是我使用的图像,如果它们有用的话(link 工作 24 小时,它们只是基本的 125x125 图像,用于盒子,X 和 O)https://picresize.com/b5deea04656747
from tkinter import *
class XOGame:
def main_game(self):
self.__game_window = Tk()
self.__grid_size = 3 # User inputted in a different part of the code
self.__game_window.title("Tic Tac Toe (" + str(self.__grid_size) + "x"
+ str(self.__grid_size) + ")")
self.build_board(self.__game_window)
self.__game_window.mainloop()
def build_board(self, window):
self.__size = self.__grid_size ** 2
self.__empty_square = PhotoImage(master=window,
file="rsz_empty.gif")
self.__squares = [None] * self.__size
# Building the buttons and gridding them
for i in range(self.__size):
self.__squares[i] = (Button(window, image=self.__empty_square))
row = 0
column = 0
number = 1
for j in self.__squares:
j.grid(row=row, column=column)
j.config(command=lambda index=self.__squares.index(j):
self.change_mark(index))
column += 1
if number % 3 == 0:
row += 1
column = 0
number += 1
# This is the part where the picture changing happens.
# I have yet to implement the turn system, thus self.__x being the only image being configured.
# This is the part with the issue.
def change_mark(self, i):
self.__x = PhotoImage(master=self.__game_window,
file="rsz_cross.gif")
self.__o = PhotoImage(master=self.__game_window,
file="rsz_nought.gif")
self.__squares[i].configure(image=self.__x, state=DISABLED)
def start(self):
# Function starts the first window of the game.
self.main_game()
def main():
ui = XOGame()
ui.start()
main()
Question: the image from the button first pressed disappears completely
您在每次点击时都会覆盖 self.__x = PhotoImage(...
,这会导致上一张图像的 垃圾收集 并且 tkinter
丢失图像引用。
只创建图像一次并像self.empty_square
一样重复使用它。
简化为:
def build_board(self, window):
self.empty_square = PhotoImage(master=window, file="empty.gif")
self.x_square = PhotoImage(master=window, file="cross.gif")
self.squares = []
for row in range(3):
for column in range(3):
self.squares.append(Button(window, image=self.empty_square))
self.squares[-1].grid(row=row, column=column)
self.squares[-1].bind("<Button-1>", self.change_mark)
def change_mark(self, event):
w = event.widget
w.configure(image=self.x_square, state=DISABLED)
我正在使用 Tkinter 制作一个可扩展的井字游戏程序。当我按下一个空的方形按钮时,图像会发生变化,这是应该的。当我按下不同的按钮时,第一次按下按钮的图像完全消失,而我希望它保留下来,唯一带有 X/O 图像的按钮是最近按下的按钮。对解决这个问题有帮助吗?我是初学者。
如果您还有关于如何进行可扩展的获胜检查的任何提示(可扩展我的意思是用户可以输入板尺寸,从 2x2 到适合屏幕的大小),那将非常有帮助。
这是我使用的图像,如果它们有用的话(link 工作 24 小时,它们只是基本的 125x125 图像,用于盒子,X 和 O)https://picresize.com/b5deea04656747
from tkinter import *
class XOGame:
def main_game(self):
self.__game_window = Tk()
self.__grid_size = 3 # User inputted in a different part of the code
self.__game_window.title("Tic Tac Toe (" + str(self.__grid_size) + "x"
+ str(self.__grid_size) + ")")
self.build_board(self.__game_window)
self.__game_window.mainloop()
def build_board(self, window):
self.__size = self.__grid_size ** 2
self.__empty_square = PhotoImage(master=window,
file="rsz_empty.gif")
self.__squares = [None] * self.__size
# Building the buttons and gridding them
for i in range(self.__size):
self.__squares[i] = (Button(window, image=self.__empty_square))
row = 0
column = 0
number = 1
for j in self.__squares:
j.grid(row=row, column=column)
j.config(command=lambda index=self.__squares.index(j):
self.change_mark(index))
column += 1
if number % 3 == 0:
row += 1
column = 0
number += 1
# This is the part where the picture changing happens.
# I have yet to implement the turn system, thus self.__x being the only image being configured.
# This is the part with the issue.
def change_mark(self, i):
self.__x = PhotoImage(master=self.__game_window,
file="rsz_cross.gif")
self.__o = PhotoImage(master=self.__game_window,
file="rsz_nought.gif")
self.__squares[i].configure(image=self.__x, state=DISABLED)
def start(self):
# Function starts the first window of the game.
self.main_game()
def main():
ui = XOGame()
ui.start()
main()
Question: the image from the button first pressed disappears completely
您在每次点击时都会覆盖 self.__x = PhotoImage(...
,这会导致上一张图像的 垃圾收集 并且 tkinter
丢失图像引用。
只创建图像一次并像self.empty_square
一样重复使用它。
简化为:
def build_board(self, window):
self.empty_square = PhotoImage(master=window, file="empty.gif")
self.x_square = PhotoImage(master=window, file="cross.gif")
self.squares = []
for row in range(3):
for column in range(3):
self.squares.append(Button(window, image=self.empty_square))
self.squares[-1].grid(row=row, column=column)
self.squares[-1].bind("<Button-1>", self.change_mark)
def change_mark(self, event):
w = event.widget
w.configure(image=self.x_square, state=DISABLED)