Scaling a Sprite Pygame: must be pygame.surface - python-2.7

code:
import pygame
pygame.init()
WIDTH = 800
HEIGHT = 480
screen = pygame.display.set_mode((WIDTH,HEIGHT))
block = pygame.sprite.Sprite()
block.image = pygame.image.load('stone.png')
newblock = pygame.transform.scale(block, (64, 64))
newblock.rect = newblock.image.get_rect()
newblock.group = pygame.sprite.GroupSingle(newblock)
newblock.group.draw(screen)
pygame.display.update()
When i try to scale the image with pygame.transform.scale it gives me this error:
Traceback (most recent call last):
File "C:/pygame/program.py", line 8, in <module>
newblock = pygame.transform.scale(block, (64, 64))
TypeError: must be pygame.Surface, not Sprite

The error message says it all. The first argument to pygame.transform.scale needs to be a Surface, but you pass a Sprite.
I guess you want something like:
import pygame
pygame.init()
WIDTH = 800
HEIGHT = 480
screen = pygame.display.set_mode((WIDTH,HEIGHT))
img = pygame.image.load('stone.png')
block = pygame.sprite.Sprite()
block.image = pygame.transform.scale(img, (64, 64))
block.rect = block.image.get_rect()
group = pygame.sprite.GroupSingle(block)
while True:
#TODO: event handling
group.draw(screen)
pygame.display.update()

Related

Trying to modify a code to capture an image every 15 minutes or so (time lapse)

Below code was taken from an existing post by Kieleth which I use as a subset of my larger codebase. I'm trying to leverage it to capture a list of frames taken once every thousand+ real time frames and later play in a time-lapse fashion. I've captured the frames but can't seem to view them when calling a simple function. I've seen in other posts that for loops are not recommended for this type of event but haven't figured out how to properly display. Any advise on this one would be appreciated?
from tkinter import ttk
import time
import cv2
from PIL import Image,ImageTk
#import threading
root = Tk()
def video_button1():# this flips between start and stop when video button pressed.
if root.video_btn1.cget('text') == "Stop Video":
root.video_btn1.configure(text = "Start Video")
root.cap.release()
elif root.video_btn1.cget('text') == "Start Video":
root.video_btn1.configure(text = "Stop Video")
root.cap = cv2.VideoCapture(0)
show_frame()
def show_frame():
# if video_btn1.cget('text') == "Stop Video":
global time_lapse_counter
ret, frame = root.cap.read()
if ret:
frame = cv2.flip(frame, 1) #flip image
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img) #converts img into tkinter readable format
root.video_label.imgtk = imgtk
if time_lapse_counter >= 20: # for Every 20 frames, capture one into time_lapse_list
time_lapse_list.append(imgtk)
time_lapse_counter = 0
root.video_label.configure(image=imgtk)
if len(time_lapse_list) == 5: # keep only 4 frames in the list *** debug purposes.
time_lapse_list.pop(0)
time_lapse_counter += 1
video_loop = root.after(40, show_frame)
else:
root.video_btn1.configure(text = "Start Video")
def time_lapse_play():
root.cap.release() #stop capturing video.
for image in time_lapse_list:
print (image, " ", len(time_lapse_list)," ",time_lapse_list) #
#*** I see the print of the pyimagexxx but nothing appears on the video***#
imgtk = image
root.video_label.imgtk = imgtk
root.video_label.configure(image=imgtk)
cv2.waitKey(500)
# video_loop = root.after(500, time_lapse_play)
def setup_widgets(): #simple label and 2 button widget setup
#Setup Top Right Window with pictures
f_width, f_height = 810, 475
root.rightframe= Frame(root, border=0, width=f_width, height = f_height)
root.rightframe.grid(row=0, column=0, padx=10, pady=0)
# Show video in Right Frame
root.cap = cv2.VideoCapture(0)
root.cap.set(cv2.CAP_PROP_FRAME_WIDTH, f_width)
root.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, f_height)
root.video_label = Label(root.rightframe)
root.video_label.grid(row=0, column = 0)
root.video_btn1 = Button(root.rightframe, fg='maroon', bg="yellow", text = "Stop Video", font=("Arial",10),height=0, width = 10, command=video_button1)
root.video_btn1.grid(row=0, column = 1)
root.video_btn2 = Button(root.rightframe, fg='maroon', bg="yellow", text="Time Lapse", font=("Arial",10),height=0, width = 10, command=time_lapse_play)
root.video_btn2.grid(row=1, column = 1)
# Main Code
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
screen_resolution = str(screen_width)+'x'+str(screen_height)
root.geometry(screen_resolution)
time_lapse_counter = 0
time_lapse_list=[]
setup_widgets()
show_frame()
root.mainloop()```
I've finally figure this one out. Seems that the for loop effectively doesn't work when using callback. I've modified the code to remove the for loop. I'm sure it could use some improvements but it works. I'm not sure how to reset the image count within the list as pop/append grows the list image number over time and I wonder about an overflow error. i.e after a few minutes, the list will contain [pyimage100 - pyimage200], after a few hours, [pyimage1000000 - pyimage1000100].
from tkinter import *
import cv2
from PIL import Image,ImageTk
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg)
import matplotlib.pyplot as plt
root = Tk()
def video_button1():# this flips between start and stop when video button pressed.
if root.video_btn1.cget('text') == "Stop Video":
root.video_btn1.configure(text = "Start Video")
root.cap.release()
elif root.video_btn1.cget('text') == "Start Video":
root.video_btn1.configure(text = "Stop Video")
root.cap = cv2.VideoCapture(0)
root.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 400)
root.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 400)
show_frame()
def show_frame():
# if video_btn1.cget('text') == "Stop Video":
global time_lapse_counter
ret, frame = root.cap.read()
if ret:
frame = cv2.flip(frame, 1) #flip image
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img) #converts img into tkinter readable format
root.video_label.imgtk = imgtk
root.video_label.configure(image=imgtk)
if time_lapse_counter >= 20: # for Every 20 frames, capture one into time_lapse_list
time_lapse_list.append(imgtk)
time_lapse_counter = 0
if len(time_lapse_list) == 100: # keep 99 frames in the list
time_lapse_list.pop(0)
time_lapse_counter += 1
video_loop = root.after(40, show_frame)
else:
root.video_btn1.configure(text = "Start Video")
def time_lapse_play():
root.cap.release() #stop capturing video.
global frame_counter
if frame_counter <= len(time_lapse_list)-1:
imgtk = time_lapse_list[frame_counter] # get image from list
root.video_label.configure(image=imgtk) # update label with image from list
frame_counter += 1
video_loop = root.after(250, time_lapse_play) #wait 250ms until next frame
else:
frame_counter = 0 #reset frame_counter
def setup_widgets(): #simple label and 2 button widget setup
#Setup Top Right Window with pictures
f_width, f_height = 1200, 500
root.rightframe= Frame(root, border=0, width=f_width, height = f_height)
root.rightframe.grid(row=0, column=0, padx=10, pady=0)
# Show video in Right Frame
root.cap = cv2.VideoCapture(0)
root.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 400)
root.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 400)
root.video_label = Label(root.rightframe)
root.video_label.grid(row=0, column = 0)
root.video_btn1 = Button(root.rightframe, fg='maroon', bg="yellow", text =
"Stop Video", font=("Arial",10),height=0, width = 10, command=video_button1)
root.video_btn1.grid(row=0, column = 1)
root.video_btn2 = Button(root.rightframe, fg='maroon', bg="yellow", text="Time Lapse", font=("Arial",10),height=0, width = 10, command=time_lapse_play)
root.video_btn2.grid(row=1, column = 1)
fig = plt.figure(1)
canvas = FigureCanvasTkAgg(fig, root.rightframe)
canvas.get_tk_widget().place(x=700,y=0)
canvas.get_tk_widget().config(border=2, bg="yellow", width=400, height=400)
# Main Code
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
screen_resolution = str(screen_width)+'x'+str(screen_height)
root.geometry(screen_resolution)
time_lapse_counter = 0
frame_counter = 0
time_lapse_list=[]
setup_widgets()
show_frame()
root.mainloop()
`

Nested frames in tkinter

Good Day,
Using tkinter, Python 3.5
I am attempting to create nested frames using for loops and lists to generate the frame names.
This works for the first level of frames within a frame, however the next level fails.
For example, this code works:
from tkinter import *
from tkinter import ttk
frameLevel1List = ['Frame1', 'Frame2', 'Frame3', 'Frame4']
frameLevel2List = ['FrameA', 'FrameB', 'FrameC', 'FrameD']
class myUI:
def __init__(self):
#create main window & frames
self.main_window = Tk()
self.main_window.title("frame2 UI V001 ")
self.main_window.configure(background='gray')
w=750
h=500
x=100
y=100
self.main_window.geometry("%dx%d+%d+%d" % (w, h, x, y))
self.userlabel = Label(self.main_window, bg='gray', fg='white', text = "user Label")
self.userlabel.pack(side="top")
self.levellabel = Label(self.main_window, bg='gray', fg='white', text = "level Label")
self.levellabel.pack(side="top")
#create bottom frame
bottomFrame = Frame(self.main_window, bg='white', height=500, width=800)
bottomFrame.pack(side=BOTTOM)
#create frames from first list
for frame in frameLevel1List:
self.frame=Frame(bottomFrame, width=800, height = 100, bg = 'green', highlightbackground="black", highlightcolor="black", highlightthickness="1")
self.frame.pack(side="top")
self.framelabel = Label(self.frame, bg='blue', fg='white', text = frame)
self.framelabel.place(x=10, y=10)
mainloop()
UI=myUI()
However, when I add a second for loop to add the second list of frames within each of the first frames, I get an error. The following code fails
from tkinter import *
from tkinter import ttk
frameLevel1List = ['Frame1', 'Frame2', 'Frame3', 'Frame4']
frameLevel2List = ['FrameA', 'FrameB', 'FrameC', 'FrameD']
class myUI:
def __init__(self):
#create main window & frames
self.main_window = Tk()
self.main_window.title("frame2 UI V001 ")
self.main_window.configure(background='gray')
w=750
h=500
x=100
y=100
self.main_window.geometry("%dx%d+%d+%d" % (w, h, x, y))
self.userlabel = Label(self.main_window, bg='gray', fg='white', text = "user Label")
self.userlabel.pack(side="top")
self.levellabel = Label(self.main_window, bg='gray', fg='white', text = "level Label")
self.levellabel.pack(side="top")
#create bottom frame
bottomFrame = Frame(self.main_window, bg='white', height=500, width=800)
bottomFrame.pack(side=BOTTOM)
#create frames from first list
for frame in frameLevel1List:
self.frame=Frame(bottomFrame, width=800, height = 100, bg = 'green', highlightbackground="black", highlightcolor="black", highlightthickness="1")
self.frame.pack(side="top")
self.framelabel = Label(self.frame, bg='blue', fg='white', text = frame)
self.framelabel.place(x=10, y=10)
#create frames from second list
for frame2 in frameLevel2List:
self.frame2=Frame(frame, width=800, height = 50, bg = 'yellow')
self.frame2.pack(side="top")
self.frame2label = Label(self.frame2, bg='blue', fg='white', text = frame2)
self.frame2label.place(x=10, y=10)
mainloop()
UI=myUI()
Here is the error message:
Traceback (most recent call last):
File "C:\Users\Nicholas Boughen\Desktop\py\rubrics\nestedFramesTest.py", line 43, in <module>
UI=myUI()
File "C:\Users\Nicholas Boughen\Desktop\py\rubrics\nestedFramesTest.py", line 36, in __init__
self.frame2=Frame(frame, width=800, height = 50, bg = 'yellow')
File "C:\Users\Nicholas Boughen\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2584, in __init__
Widget.__init__(self, master, 'frame', cnf, {}, extra)
File "C:\Users\Nicholas Boughen\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2132, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Users\Nicholas Boughen\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2110, in _setup
self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'
Anything that could help me understand what I have done wrong and create nested frames with for loops would be most appreciated.
Perhaps there's a completely different way of procedurally creating nested frames that would be better? What I'm trying to do is to generate the frame names from a list and change the interface as the list changes. So if more or fewer items are in the list, there will be more or fewer frames in the interface. The list will be edited from a different interface.
lor
After many more hours of researching other people who had similar issues, and #jasonharper comment, I have discovered that I need to save(append) the frame ID into a list as it is created, to ensure each widget remains unique.
Here is the code that does what I want it to do:
from tkinter import *
import functools
class practice:
def __init__(self,root):
self.frame_list = []
for w in range(5):
frame = Frame(root, width=800, height = 100, bg = 'green', highlightbackground="black", highlightcolor="black", highlightthickness="1")
frame.pack(side="top")
self.frame_list.append(frame)
for w in range(5):
frame = Frame(root, width=400, height = 50, bg = 'blue', highlightbackground="black", highlightcolor="black", highlightthickness="1")
frame.pack(side="top")
self.frame_list.append(frame)
for w in range(5):
frame = Frame(root, width=200, height = 25, bg = 'yellow', highlightbackground="black", highlightcolor="black", highlightthickness="1")
frame.pack(side="top")
self.frame_list.append(frame)
print('button list is', self.frame_list)
root = Tk()
root.title("frame2 UI V001 ")
root.configure(background='gray')
w=750
h=500
x=100
y=100
root.geometry("%dx%d+%d+%d" % (w, h, x, y))
Thank you, I will attempt to post questions with greater clarity in future.
lor

how to insert buttons on the west side an Image file on the East Side or simply as a background using Tk

Here is my script
# Python 2.7.14 version
from Tkinter import *
import Tkinter
import tkMessageBox
from urllib2 import urlopen
import PIL
from PIL import ImageTk
import ImageTk
FILENAME = 'Fleur_de_lys.jpg'
root = Tk()
background = Canvas(root, width=250, height=250)##AttributeError: class Tk has no attribute 'Canvas'
canvas.pack()
tk_img = ImageTk.PhotoImage( file = FILENAME)
canvas.create_image(125, 125, image=tk_img)
quit_button = tk.Button(root, text = "Quit", command = root.quit, anchor = 'w',
width = 10, activebackground = "#33B5E5")
quit_button_window = canvas.create_window(10, 10, anchor='nw', window=quit_button)
root.mainloop()
No matter what attempt I do I keep getting AttributeError: class Tk has no attribute 'Canvas' where is my error if I just create button I have no issue what so ever all work but when I attempt to have a background image everything does not work
The problem appears to be that you're creating a canvas, and storing it in a variable named background, yet the very next line you are trying to call something called 'canvas', which you've never created.
Change this:
background = Canvas(root, width=250, height=250)
canvas.pack()
...
canvas.create_image(125, 125, image=tk_img)
to this:
background = Canvas(root, width=250, height=250)
background.pack()
...
background.create_image(125, 125, image=tk_img)
I fixed the issue jpg image was not being accepted I made it with a gif. Also found how to correct the buttons location and posn.
Here is the example without all the program
canvas = Canvas(root, width = 500, height = 500, bg='black')
canvas.pack(expand=YES, fill=BOTH)
my_image = PhotoImage(file='C:\\MOTD\\fleur_de_lys.gif')
canvas.create_image(0, 0, anchor = NW, image=my_image)
b1 = Button(root, text="From the Commander", command=callback,anchor = 'w',
width = 18, activebackground = "#33B5E5")
button_window1 = canvas.create_window(10, 10, anchor='nw', window=b1)

Issue with script using Pygame

I'm writing a small game in my spare time. This is what I have so far:
from pygame import * #import relevant modules
from PIL import Image
import os, sys
init() #initialise
class sprite:
def __init__(self, object, x = 0, y = 0, w = 0, h = 0):
self.image = image.load(object).convert()
self.posx = x
self.posy = y
self.position = ((x, y, w, h))
def resize(self, sh, sw):
self.image = transform.scale(self.image, (sh, sw))
return self.image
def move(self, window, background, right, down):
self.posx = x + right
self.posy = y + down
window.blit(background, self.position, self.position)
self.position.move(right, down)
window.blit(self, self.position)
window.update()
os.chdir('C:\\Users\\alesi\\Documents\\Pygame\\Project\\') #current folder change
win = display.set_mode((736, 552))#load window
Clock = time.Clock() #handy clock
background = image.load('background.jpg').convert()#load images
player = sprite('ball.png', 350, 275, 20, 20)
player = player.resize(20, 20)
win.blit(background, (0, 0))
win.blit(player, (350, 275))
display.update()
while True:
event.pump()
keys = key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
elif keys[K_RIGHT]:
player.move(win, background, 20, 0)
elif keys[K_LEFT]:
player.move(win, background, -20, 0)
elif keys[K_DOWN]:
player.move(win, background,0, 20)
elif keys[K_UP]:
player.move(win, background, 0, -20)
In short, it should create an object on a background and allow you to move the object using the arrow keys. However, I get the error:
C:\Users\alesi\Documents\Pygame\Project>python2 game2.py
Traceback (most recent call last):
File "game2.py", line 51, in <module>
player.move(win, background, -20, 0)
AttributeError: 'pygame.Surface' object has no attribute 'move'
I'm struggling to understand why my player instance of the sprite class is not recognising the move method. Also, I'm confused by why during the win.blit() function, I have to give the argument player instead of player.image, the attribute which I've stored the image.
Any advice would be appreciated.
In code
def resize(self, sh, sw):
self.image = transform.scale(self.image, (sh, sw))
return self.image
you returns image which is Surface instance - so in line
player = player.resize(20, 20)
you replace sprite instance with Surface instance
But you don't have to assign it to player again.
Do:
def resize(self, sh, sw):
self.image = transform.scale(self.image, (sh, sw))
# without return
# without player =
player.resize(20, 20)
After that player.move(...) will work again.
And again you will have to use player.image in blit()

Clicking on images with PyGame

I am currently developing a game using Python and PyGame. I have made image sprites for the main option buttons, but I cannot seem to figure out how to make the images clickable which will take me to a different screen. Source code:
import os
import sys
import pygame
import time
class Colors:
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
dark_red = (102, 0, 0)
grey = (128, 128, 128)
dark_grey = (51, 51, 51)
class Variables:
screen_w = 1000
screen_h = 600
battleButton = pygame.image.load("Images/battle_button.png")
shopButton = pygame.image.load("Images/shop_button.png")
saveButton = pygame.image.load("Images/save_button.png")
equippedItem = pygame.image.load("Images/equipped.png")
creditsButton = pygame.image.load("Images/credits_button.png")
baseballBat = pygame.image.load("Images/baseball_bat.png")
baseballBat = pygame.transform.scale(baseballBat, (250, 250))
playerHealthMax = 100
playerHealthMin = 0
playerHealth = playerHealthMax
playerCash = 0
pygame.font.init()
gameFont = pygame.font.SysFont("Tweaky", 70)
title = gameFont.render("Causatum", 1, (Colors.black))
mainFont = pygame.font.SysFont("Arial Rounded MT Bold", 50)
equippedWeap = mainFont.render("Equipped", 1, (Colors.black))
cash = mainFont.render("Cash: ${}".format(playerCash), 1,
(Colors.black))
health = mainFont.render("Health: {}".format(playerHealth), 1,
(Colors.black))
gameVersion = mainFont.render("v1.0.0", 1, (Colors.black))
screen = pygame.display.set_mode((Variables.screen_w, Variables.screen_h))
pygame.display.set_caption("Causatum")
screen.fill(Colors.black)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(Colors.grey)
screen.blit(Variables.battleButton, (0, 170))
screen.blit(Variables.shopButton, (0, 240))
screen.blit(Variables.saveButton, (0, 310))
screen.blit(Variables.title, (330, 0))
screen.blit(Variables.equippedItem, (645, 250))
screen.blit(Variables.baseballBat, (735, 230))
screen.blit(Variables.equippedWeap, (723, 215))
screen.blit(Variables.creditsButton, (0, 380))
screen.blit(Variables.cash, (0, 50))
screen.blit(Variables.health, (0, 0))
screen.blit(Variables.gameVersion, (905, 0))
pygame.display.flip()
Any kind of help is appreciated. Thanks.
Try something like this:
#in 'Variables' add this line
battleButton_rect = battleButton.get_rect(centerx = 0, centery = 170)
#new function, outside any classes
def collide(mouseX, mouseY, rect): #new function for checking the collision of the mouse with a button
return (mouseX >= rect.x-rect.width/2 and mouseX <= rect.x+rect.width/2 and mouseY >= rect.y-rect.height/2 and mouseY <= rect.y+rect.height/2)
# in 'while running'
if pygame.mouse.get_pressed()[0]: #0 for left button, 1 for right, 2 for middle
mouse_pos = pygame.mouse.get_pos()
if collide(mouse_pos[0], mouse_pos[1], Variables.battleButton_rect):
#code for battle button clicked here
#replace
screen.blit(Variables.battleButton, (0, 170))
#with
screen.blit(Variables.battleButton, Variables.battleButton_rect)
This, of course, is only for the battle button. You need to add a Rect for every button you use. Also, I would suggest making a button class with the collision function inside and having an array of those buttons that you can iterate through without convoluting your code. But the above is a quick and dirty solution.
Hope this helps!