I am creating a game with Tkinter. I created a chess-board and I wanted to add the pieces in the board. However, when I add the image it doesn't appear.
I DID read about the garbage-collector issue and tried different things. For example making the image a global or to make it a class variable.
This is my code:
import tkinter as tk
LARGE_FONT = ("Verdana", 12)
WIDTH = 1200
HEIGHT = 750
AREA_WINDOW = str(WIDTH)+"x"+str(HEIGHT) # 1200x700
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
# *args = arguments. Pass as many variables as you want
# kwargs = keyword arguments. Pass dictionaries.
tk.Tk.__init__(self, *args, **kwargs) # Let's initialize tkinter.
container = tk.Frame(self) # It will contain everything of the app.
container.pack(side="top", fill="both", expand="True")
# Fill tells the widget to grow to as much space is available for it in the direction specified
# BOTH means that the widget should expand both horizontally and vertically
# The expand is beyond the limits that you gave, while fill is the limits you said.
container.grid_rowconfigure(0, weight=1) # Weight 2 will grow twice as 1
container.grid_columnconfigure(0, weight=1) # We want equal so we leave at 1.
self.frames = {} # Empty dictionary.
for Frame in (StartPage, Game):
frame = Frame(container, self)
self.frames[Frame] = frame
frame.grid(row=0, column=0, sticky="nsew") # Align and stretch everything to the sides of the window.
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
global client
tk.Frame.__init__(self, parent)
text = tk.Label(self, text="Welcome!").pack()
button = tk.Button(self, text="Play!", command=lambda: controller.show_frame(Game)).pack()
class Game(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
text = tk.Label(self, text="Play!").pack();
self.board = tk.Canvas(self, height = 640, width = 640, bg = "white")
self.create_board()
self.board.pack()
self.image = None
def create_board(self):
# for i in range(0,4):
# for j in range(0, 4):
# square = self.board.create_polygon((80 + 2*i*80, 0 + 2*j*80), (80 + 2*i*80, 80 + 2*j*80),
# (160 + 2*i*80, 80 + 2*j*80), (160 + 2*i*80, 0 + 2*j*80), fill='gray')
# square = self.board.create_polygon((0 + 2*i*80, 0 + 2*j*80), (0 + 2*i*80, 80 + 2*j*80),
# (80 + 2*i*80, 80 + 2*j*80), (80 + 2*i*80, 0 + 2*j*80), fill='ivory2')
# square = self.board.create_polygon((0 + 2*i*80, 80 + 2*j*80), (0 + 2*i*80, 160 + 2*j*80),
# (80 + 2*i*80, 160 + 2*j*80), (80 + 2*i*80, 80 + 2*j*80), fill='gray')
# square = self.board.create_polygon((80 + 2*i*80, 80 + 2*j*80), (80 + 2*i*80, 160 + 2*j*80),
# (160 + 2*i*80, 160 + 2*j*80), (160 + 2*i*80, 80 + 2*j*80), fill='ivory2')
global filename
filename = tk.PhotoImage(file="pieces.gif")
self.image = self.board.create_image(2000, 1000, anchor=tk.CENTER, image=filename)
app = Application()
app.geometry(AREA_WINDOW)
app.title("Application")
app.resizable(width=False, height=False)
app.mainloop()
Maybe the issue is the size of the picture when I write (2000, 1000,...)? It could be, however, I tried different numbers and nothing changed. The GIF is a 2,000x667 file.
Thank you for your help.
