python - tkinter tic tac toe program -
i experimenting tkinter , thought of implementing simple tic-tac-toe game. here came with
import tkinter tk class gui(tk.frame): def __init__(self, master): super().__init__(master) self.parent = master self.parent.title("tic tac toe") logo = tk.photoimage(file="x.png") in range(3): j in range(3): w = tk.label(self,image=logo) w.grid(row=i, column=j) self.pack() if __name__ == '__main__': root = tk.tk() logo = tk.photoimage(file="x.png") f = gui(root) root.mainloop()
when execute nothing being displayed. have image in current folder. verify if doing right changed main part to:
if __name__ == '__main__': root = tk.tk() logo = tk.photoimage(file="x.png") f = gui(root) in range(3): j in range(3): w = tk.label(f,image=logo) w.grid(row=i, column=j) f.pack() root.mainloop()
by commenting respective code in gui class , works. can tell me why so? ve spent hours trying figure out.
keep reference photoimage
not garbage collected. saving object instance variable solve issue:
def __init__(self, master): super().__init__(master) self.parent = master self.parent.title("tic tac toe") self.logo = tk.photoimage(file="x.png") # <---- in range(3): j in range(3): w = tk.label(self, image=self.logo) # <--- w.grid(row=i, column=j) self.pack()
Comments
Post a Comment