A new image for a widget does not appear - python

I have 2 classes, first one is Game where's located the main window and its settings, the second one is Piece that have some parameters to establish a Button with an image on its surface using Pillow. From the main class Game I planned call the other class to create objects on this window(root). Everything works fine(1st pic) untill I call my setter from any of these classes to change the image of the Button(Piece), it doesn't appear though the path to the new file is absolutely correct and the button becomes enabled if I'm not wrong and it's not clickable (2nd pic)
How can I resolve the problem, cos I need this possibility to change pictures on the button for my tic-toe game?
from tkinter import *
from PIL import Image
from PIL import ImageTk
class Game:
ICON = "./rsc/logo.ico"
KREST_IMAGE = "./rsc/krest.png"
DEFAULT_IMAGE = "./rsc/1.png"
def __init__(self):
self.root = Tk()
self.root.title("Tic-tac-toe")
self.root.iconbitmap(Game.ICON)
self.root.resizable(False,False)
self.root.geometry("300x300+500+500")
self.c = Piece()
self.root.mainloop()
class Piece:
def __init__(self):
self.img = Game.DEFAULT_IMAGE
self.piece = Button(image = self._img)
self.img = Game.KREST_IMAGE
# for ex. calling again the setter. Writing direct path instead of Game.KREST_IMAGE doesn't help.
self.piece.pack()
#property
def img(self):
return self._img
#img.setter
def img(self, path):
self.temp = Image.open(path)
self.temp = self.temp.resize((20,20), Image.ANTIALIAS)
self.temp = ImageTk.PhotoImage(self.temp)
self._img = self.temp
the picture

Related

tkinter.TclError: image "pyimage" doesn't exist with Button wrapper class

A common problem with Tkinter is that in order to use images in Labels and Buttons, you need a reference to the PhotoImage object... somehow.
I have written a wrapper class around Button to add my own functionalities, because I want to use GIFs instead of images, and I want to be able to switch between gifs when I press the button (or use a keyboard hotkey). The first GIF runs fine and loops perfectly. When I switch to the second GIF, I get the error message, saying _tkinter.TclError: image "pyimage48 ... pyimage55" doesn't exist. It looks like the following:
from tkinter import *
from PIL import ImageTk, Image
class AnimatedButton(Button)
def __init__(self, master, size, img_paths):
self.size = size
self.seq_count = len(img_paths) # Number of gif files
self.sequences = []
for path in img_paths:
gif, delay = loadGif(path)
# Create a tuple of all frames in a gif, with the delay between frames. Store this tuple in self.sequences
self.sequences.append(([ImageTk.PhotoImage(frame) for frame in gif], delay))
self.delay = self.sequences[0][1]
self.current_sequence = self.sequences[0][0]
self.image = self.current_sequence[0]
self.seq_id = 0 # Sequence counter
self.frame_id = 0 # Frame counter
Button.__init__(self, master, image=self.image, width=size, height=size)
self.cancel = self.after(self.delay, self.play)
def play(self):
self.image = self.current_sequence[self.frame_id]
self.config(image=self.image)
# More stuff below to loop through the frames etc.
What is strange is that I don't have any of this with my other Button class, MyButton, also a wrapper class.
class MyButton(Button):
def __init__(self, master, size, img_paths):
self.image_count = len(img_paths)
self.image_id = 0
self.size = size
self.images = []
for path in img_paths:
try:
im = Image.open(path)
except:
print("Could not open file {}".format(path))
photo_image = ImageTk.PhotoImage(im, image_mode)
self.images.append(photo_image)
self.image = self.images[0]
Button.__init__(self, master, image=self.image, width=size,
height=size)
Most Google searches came up with the fact that you shouldn't use two tkinter.Tk() calls, but I am only using one (Yes, I made sure).
Any ideas are very much appreciated!
Thanks to the hint by stovfl in the comments above, I was missing [0] in play():
Correct code should be:
def play(self):
self.image = self.current_sequence[self.frame_id][0]
self.config(image=self.image)
# More stuff below to loop through the frames etc.

How to solve Error: image "pyimage1" doesn't exist

I wanted to program a simple graphical version of Tic Tac Toe in Python after having previously made a text based one with a friend. For this I used tkinter. I have been able to get rid of all mistakes in the first window where you choose which symbol you want. But in the second window when you are supposed to place it, when I press one of the buttons it tells me that "pyimage2" doesn't exist which seems to be a common error
I have already checked out some other threads where I was told that I should use TopLevel instead, since there can only be one instance of Tk(). But I already am using it, and when I try using two instances by using destroy() on the first, the error remains. I have also switched from simple PhotoImage to PIL, but even that is of no help
from tkinter import *
from random import *
from PIL import Image, ImageTk
global root
global SpielerSymbol
SpielerSymbol = "Defaultshit"
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
class SelectionScreen:
def __init__(self,pRoot):
Imageo = Image.open("o.png")
o = ImageTk.PhotoImage(Imageo)
Imagex = Image.open("x.png")
x = ImageTk.PhotoImage(Imagex)
Screen = Toplevel(pRoot)
self.TextField = Label(Screen, text="Please choose a symbol.")
self.TextField.grid(row=1,column=1)
self.ButtonX = self.SelectionButton(x,Screen)
self.ButtonX.Choice.grid(row = 2, column = 1)
self.ButtonO = self.SelectionButton(o,Screen)
self.ButtonO.Choice.grid(row = 2, column = 2)
Screen.mainloop()
class SelectionButton:
def __init__(self, pImage, pScreen):
self.Choice = Button(pScreen, image = pImage, command = lambda: combine_funcs(setSpielerSymbol(str(pImage)), pScreen.destroy(), pScreen.quit()))
def setSpielerSymbol(pZeichen):
global SpielerSymbol
SpielerSymbol = pZeichen
class Game:
def __init__(self, pRoot):
global SpielerSymbol
ImageFeldx = Image.open("Feldx.png")
Feldx = ImageTk.PhotoImage(ImageFeldx)
ImageFeldo = Image.open("Feldo.png")
Feldo = ImageTk.PhotoImage(ImageFeldo)
ImageFeld = Image.open("Feld.png")
Feld = ImageTk.PhotoImage(ImageFeld)
Window = Toplevel(pRoot)
Feld1 = [self.Feld(Feld,Window,1,1), self.Feld(Feld,Window,1,2), self.Feld(Feld,Window,1,3),
self.Feld(Feld,Window,2,1), self.Feld(Feld,Window,2,2), self.Feld(Feld,Window,2,3),
self.Feld(Feld,Window,3,1), self.Feld(Feld,Window,3,2), self.Feld(Feld,Window,3,3)]
Window.mainloop()
class Feld:
def __init__(self, pImage, pWindow, pRow, pColumn):
self.Feld1 = Button(pWindow, image = pImage, command =lambda: combine_funcs(self.setFeldImage(self), Window.quit()) )
self.Feld1.grid(row=pRow,column=pColumn)
def setFeldImage(self, pFeld1):
pFeld1.Feld1.config(image=SpielerSymbol)
def main():
root = Tk()
root.withdraw()
SelectionScreen1 = SelectionScreen(root)
print("Das ist das Werk von Feiglingen")
Game1 = Game(root)
main()
The output should be two windows, first the one where you choose a symbol, that one should work fine, and the second should be a tic tac toe field where clicking on a button should display the symbol you've chosen. And what I instead get is the error message image "pyimage1" doesn't exist
Also sorry for the ugly code, I am still a beginner, especially at Python

I cannot open an image using a canvas and create_image on tkinter

I am making a game for the repl.it game jam. I am trying to put an image on a canvas that was created in a different class from the class that the image is being created on. The canvas has successfully displayed text and a button with the image that I need, but create_image is not working.
This might be an issue with resolution, but I cannot check that right now, the image is 1920 x 1080 and so is the game.
I've already tried create_image.
Full program (including images)
class game(Application):
"""Where game elements are to be held"""
def __init__(self, players, location, sWidth, sHeight, canvas):
"""Initiate variables & start game"""
#Initiate variables
self.players = players
self.location = location
self.screenWidth = sWidth
self.screenHeight = sHeight
self.Canvas1 = canvas
self.LOCATIONS = Application.LOCATIONS
self.font = Application.font
#Gathering Images
self.map1BG = PhotoImage(file = "polasib.gif")
#Debugging
print("Loading Map", self.location\
, "\nPlayers:", self.players)
self.createLevel(self.location)
def createUI(self, players):
"""Creates the UI that all levels will have"""
self.Canvas1.create_text(self.screenWidth/2, self.screenHeight/16, fill = "white", \
font = (self.font, self.screenWidth//34), text = self.LOCATIONS[self.location - 1])
def createLevel(self, location):
"""Creates the elements of the level"""
if self.location == 1:
#Polasi b
print("Creating Polasi b Level")
self.createUI(self.players)
self.Canvas1.create_image(self.screenWidth/2, self.screenHeight/2, image = self.map1BG, \
anchor = NW)
Expectation: I expect the image to load (and that it will require some realignment)
Result: No image appears but everything else added (as a test) works.
Since you did not save the reference to the instance of game, it will be destroyed after exiting Application.gameScreen(). Therefore the reference of the image of create_image will be lost and no image will be shown. Try assigning the instance of game to an instance variable of Application, like below:
self.running_game = game(self.players, self.mapChosen, self.screenWidth, self.screenHeight, self.Canvas1)

Tkinter canvas PhotoImage is not appearing

I have a problem with my code. I am creating a small video game called Lumanite. I have created the homepage and have started the graphics generation, but I have run into a bug. I am using Python 3.3 and am on a Win 10 laptop. I run the program through a run file, which accesses the main_game file that uses the classes outlined in a separate file, spritesclasses. I am trying to make a sprite appear. Here is the code for the main_game file and the spritesclasses file. (They import the canvas and root from a MENU file)
#SPRITES
from tkinter import *
from GUI_FILE import canvas, root
from OPENING_FILE import show, hide
class Sprite():
def __init__(self, photoplace):
self.orgin = photoplace
self.photo = PhotoImage(file=photoplace)
self.w = self.photo.width()
self.h = self.photo.height()
def draw(self):
self.sprite = canvas.create_image(self.h, self.w, image=self.photo)
And the MAIN_GAME file:
#Main Game File:
from tkinter import *
from OPENING_FILE import show, hide
from GUI_FILE import root, canvas
from spritesclasses import *
def start_game():
genterrain()
def genterrain():
test = Sprite("logo.gif")
test.draw()
And the sprites are not appearing. No error or anything. Please help me. I will supply you with information at a further notice.
This is a known but tricky issue. You can read about it in Why do my Tkinter images not appear? I've implemented one possible solution below:
from tkinter import *
class Sprite():
def __init__(self, photoplace):
self.photo = PhotoImage(file=photoplace)
self.w = self.photo.width()
self.h = self.photo.height()
self.sprite = None
def draw(self):
canvas = Canvas(root, width=self.w, height=self.h)
canvas.pack()
self.sprite = canvas.create_image(0, 0, anchor=NW, image=self.photo)
def start_game():
genterrain()
def genterrain():
sprite = Sprite("logo.gif")
sprite.draw()
sprites.append(sprite) # keep a reference!
root = Tk()
sprites = []
start_game()
root.mainloop()
The assignment self.photo = PhotoImage(file=photoplace) isn't a sufficient reference as the object test goes out of scope when genterrain() returns and is garbage collected, along with your image. You can test this by commenting out the line sprites.append(sprite) and see your image disappear again.
Also, it wasn't clear why you were positioning the image at it's own width and height -- the first to arguments to create_image() are the X and Y position. I moved canvas creation into draw() so I could size the canvas to the image but that's not a requirement of the visibility fix.

Displaying TkInter and OpenCV windows on the same time

I am working to extend this solution given to me previously.
The user can draw randomly using the mouse on a TkInter Canvas widget, after that, the same curves with the same pixels coordinates are drawn on the OpenCV window.
I want to modify that solution so that the TkInter Canvas and the OpenCV window must be shown in the same time: each time the suer finishes drawing one curve on TkInter it is immediately redrawn on the OpenCV window.
Is there a way to fulfill this goal ?
Thank you in advance for any help.
This is arguably even easier to do than to draw all lines in OpenCV later.
I'd make the MaClasse class only make a blank image, open a window and show it on initiation, and give it a method to draw a single line and show the image again.Then you can create a MaClasse object in Test and draw a line in OpenCV each time you draw a line in Tkinter. Then you won't even need to save all lines you've drawn (you can completely remove self.liste).
from Tkinter import *
import numpy as np
import cv2
class Test:
def __init__(self):
self.b1="up"
self.xold=None
self.yold=None
self.liste=[]
self.maclasse = MaClasse()
def test(self,obj):
self.drawingArea=Canvas(obj)
self.drawingArea.pack()
self.drawingArea.bind("<Motion>",self.motion)
self.drawingArea.bind("<ButtonPress-1>",self.b1down)
self.drawingArea.bind("<ButtonRelease-1>",self.b1up)
def b1down(self,event):
self.b1="down"
def b1up(self,event):
self.b1="up"
self.xold=None
self.yold=None
self.liste.append((self.xold,self.yold))
def motion(self,event):
if self.b1=="down":
if self.xold is not None and self.yold is not None:
event.widget.create_line(self.xold,self.yold,event.x,event.y,fill="red",width=3,smooth=TRUE)
self.maclasse.dessiner_ligne(self.xold,self.yold,event.x,event.y)
self.xold=event.x
self.yold=event.y
self.liste.append((self.xold,self.yold))
class MaClasse:
def __init__(self):
self.s=600,600,3
self.ma=np.zeros(self.s,dtype=np.uint8)
cv2.namedWindow("OpenCV",cv2.WINDOW_AUTOSIZE)
cv2.imshow("OpenCV",self.ma)
def dessiner_ligne(self, xold, yold, x, y):
cv2.line(self.ma,(xold, yold),(x,y),[255,255,255],2)
cv2.imshow("OpenCV",self.ma)
if __name__=="__main__":
root = Tk()
root.wm_title("Test")
v = Test()
v.test(root)
root.mainloop()
Since the above code using a Tkinter window and a OpenCV window does not work for you, you could also show the OpenCV image in a Tkinter Toplevel window.
from Tkinter import *
import numpy as np
import cv2
import Image, ImageTk
class Test:
def __init__(self, parent):
self.parent = parent
self.b1="up"
self.xold=None
self.yold=None
self.liste=[]
self.maclasse = MaClasse(self.parent)
def test(self):
self.drawingArea=Canvas(self.parent)
self.drawingArea.pack()
self.drawingArea.bind("<Motion>",self.motion)
self.drawingArea.bind("<ButtonPress-1>",self.b1down)
self.drawingArea.bind("<ButtonRelease-1>",self.b1up)
def b1down(self,event):
self.b1="down"
def b1up(self,event):
self.b1="up"
self.xold=None
self.yold=None
self.liste.append((self.xold,self.yold))
def motion(self,event):
if self.b1=="down":
if self.xold is not None and self.yold is not None:
event.widget.create_line(self.xold,self.yold,event.x,event.y,fill="red",width=3,smooth=TRUE)
self.maclasse.dessiner_ligne(self.xold,self.yold,event.x,event.y)
self.xold=event.x
self.yold=event.y
self.liste.append((self.xold,self.yold))
class MaClasse:
def __init__(self, parent):
self.s=600,600,3
self.ma=np.zeros(self.s,dtype=np.uint8)
self.top = Toplevel(parent)
self.top.wm_title("OpenCV Image")
self.label = Label(self.top)
self.label.pack()
self.show_image()
def dessiner_ligne(self, xold, yold, x, y):
cv2.line(self.ma,(xold, yold),(x,y),[255,255,255],2)
self.show_image()
def show_image(self):
self.im = Image.fromarray(self.ma)
self.imgtk = ImageTk.PhotoImage(image=self.im)
self.label.config(image=self.imgtk)
if __name__=="__main__":
root = Tk()
root.wm_title("Test")
v = Test(root)
v.test()
root.mainloop()

Categories