pygame my picture wont show up on my window - python-2.7

import pygame
import sys
import os
from pygame.locals import *
#Allows for the editing of a window
pygame.init()
#Sets screen size
window = pygame.display.set_mode((800,600),0,32)
#Names the window
pygame.display.set_caption("TEST")
#Types of colors (red,green,blue)
black = (0,0,0)
blue = (0,0,255)
green = (0,255,0)
yellow = (255,255,0)
red = (255,0,0)
purple = (255,0,255)
lightblue = (0,255,255)
white = (255,255,255)
pink = (255,125,125)
mif="climb.PNG"
mif=pygame.image.load(mif).convert()
#Loop
gameLoop = True
while gameLoop:
pygame.display.flip() #must flip the image o the color is visable
window.blit(mif, (0,0))
window.fill(black) #used to fill the creen with the certian color variables
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameLoop=False #Allows the user to exit the loop/game
pygame.quit() #quit the pygame interface
exit(0)
When I select run my yellow window pops up but no image...
I'm new to pygame and python so maybe I just did something wrong?
Is there a specific place that I should have my image saved to?
I'm running on a windows 8 hp ENVY...
I just want to learn what I'm doing wrong so I don't make the error again. (I should also add that I have tried many different pictures and some where not able to be located.)

First fill window with black color window.fill(black),
then put image on it window.blit(mif, (0,0)),
and last send it to your graphic card (and on your monitor) pygame.display.flip()
window.fill(black)
window.blit(mif, (0,0))
pygame.display.flip()
If your image is as big as window then you don't need to use fill(black).
You can keep image in the same folder with script. Or you can create subfolder only for image to have order in folders.
If you call this folder images then you have to add this to filenames images/climb.PNG.
BTW: better use / than \ in path because \n in path can be interpreted as ENTER/new line as in any text.
But the most properly is:
import os
filename = os.path.join('images', 'climb.PNG')

Related

Tkinter Background not showing transparent

Hey im a newbie in python in my code there is a animated gif that play whenever i run the code. The gif image is actually transparent when open in adobe etc. but the problem is when i run the code.The frame which is the color gray included in the gif.I want to remove the color gray so that the only thing that can see is my only animated gif
This is my code:
# mimic an animated GIF displaying a series of GIFs
# an animated GIF was used to create the series of GIFs
# with a common GIF animator utility
import time
from Tkinter import *
root = Tk()
imagelist = ["dog001.gif","dog002.gif","dog003.gif"]
# extract width and height info
photo = PhotoImage(file=imagelist[0])
width = photo.width()
height = photo.height()
canvas = Canvas(width=width, height=height)
canvas.create_line(0,240,640,240, fill='blue')
canvas.pack()
# create a list of image objects
giflist = []
for imagefile in imagelist:
photo = PhotoImage(file=imagefile)
giflist.append(photo)
# loop through the gif image objects for a while
for k in range(0, 10):
for gif in giflist:
canvas.delete(ALL)
canvas.create_image(width/2.0, height/2.0, image=gif)
canvas.update()
time.sleep(0.1)
root.mainloop()[![enter image description here][1]][1]
dog001.gif
dog002.gif
dog003.gif
As described here, making a Tkinter window transparent is possible, but depends on your OS.
If you are on Windows, just add the following lines after creating the root:
root = Tk()
# Hide the root window drag bar and close button
root.overrideredirect(True)
# Make the root window always on top
root.wm_attributes("-topmost", True)
# Define a transparent color
root.wm_attributes("-transparentcolor", '#eeefff')
Then set your canvas' background color as the transparent color defined above:
canvas = Canvas(width=width, height=height, bg='#eeefff', highlightthickness=0)

pygame can't proceed successfully

import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((600,170),0,32)
pygame.display.set_caption("Hello World!") //set caption
background = pygame.image.load('bg.jpeg').convert //load picture and convert it
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(background,(0,0))
pygame.display.update() //refresh
The Error I get :
File "/Users/huangweijun/PycharmProjects/untitled1/first.py", line 12, in<module>
screen.blit(background,(0,0))
TypeError: argument 1 must be pygame.Surface, not builtin_function_or_method
i have download pygame
i don't know how to solve this issue.
The first argument of the function screen.blit is a pygame Surface. Think of this as a screen to draw to. You are specifying an object of the class Image. This will not work for you cannot draw to an image.
Replace background with screen and add background as an argument between screen and (0,0). You code should now look like this:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((600,170),0,32)
pygame.display.set_caption("Hello World!") //set caption
background = pygame.image.load('bg.jpeg').convert //load picture and convert it
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(screen,background,(0,0))
pygame.display.update() //refresh
pineapple, Looks like the issue here is in this line:
background = pygame.image.load('bg.jpeg').convert
I think what you wanted to use was:
background = pygame.image.load('bg.jpeg').convert_alpha()
Hopefully that's what you were looking for!
EDIT: You could have also just added parentheses after the convert() Oops!
Try them both and see what happens!
-Travis

put label in frame without class etc

How can I get the label in this simple frame? I tried to make the label go to the frame but then I only see the label and the frame disappears.
Notice here that I want 2 different sizes. The root.geometry("200x200") which is gray. I also am putting a Frame in the root of 100w x 100h. Run the code, I can see both color areas great. But my label does not appear, in the Frame darkgray area. Which is my problem.
from Tkinter import *
root=Tk()
root.geometry("200x200")
root.title("basicGUI")
root.configure(background="gray")
frame = Frame(width=100, height=100,
bg="darkgray")
frame.pack()
mylabel=Label(text="anyWord")
mylabel.pack()
"""
# darkgray frame disappears
mylabel=Label(frame, text="anyWord")
mylabel.pack(side = LEFT)
"""
mainloop()
Thanks to Lafexlos, a one line addition solved my problem.
from Tkinter import *
root=Tk()
root.geometry("200x200")
root.title("basicGUI")
root.configure(background="gray")
frame = Frame(width=100, height=100,
bg="darkgray")
frame.pack_propagate(0) #adding line solves problem
frame.pack()
mylabel1=Label(frame, text="anyWord")
mylabel1.pack(side = TOP)
mainloop()

Pygame - pygame.mouse.get_pos problems

The problem isn't errors, it's just that i'm trying to get this crosshair image to follow the mouse about the screen, I succeeded but it's leaving a trail of the same image behind it so the screen eventually becomes spammed with the image, heres the code:
import sys, pygame;
from pygame.locals import *;
spaceship = ('spaceship.png')
mouse_c = ('crosshair.png')
pygame.init()
screen = pygame.display.set_mode((800, 600))
mousec = pygame.image.load(mouse_c).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN and event.button == 1:
print("test1")
elif event.type == MOUSEBUTTONDOWN and event.button == 3:
print("test3")
pos = pygame.mouse.get_pos() #Here is the mouse following code
screen.blit(mousec, (pos)) #Setting image to pos of mouse.. atleast I think I am
pygame.display.update()
Your code has no problem....it is doing what you are telling it.
However for any image drawn a new background is created (along with the image) that is set as the new background it self so the background needs to be erased and set to default before the function pygame.display.update() or pygame.display.flip() is called.
more information here
So basically all you can do is create a new background by taking a image (800x600) ('background.png').
Load it:
bk = pygame.image.load('background.png').convert_alpha()
Blit it before blitting other moving objects.screen.blit(bk, (0,0))pos = pygame.mouse.get_pos() #Here is the mouse following code
screen.blit(mousec, (pos)) #Setting image to pos of mouse
pygame.display.update()
Adding a background to your game provides the game a complete touch but if you do not want to add it then the simplest thing you can do is fill your screen with a color every time the while loop is called.
So just after the while loop starts add this:
screen.fill(Color("black"))

multiple tasks on tkinter canvas?

I am trying to update canvas background at the same time running a binding event.
(from code)In do_popup popup menu will be implemented and conti will continuously change the canvas background color. how can i use popup option while canvas is updating continuously.
Sample code:
from Tkinter import *
root = Tk()
def do_popup(event,w2):
print w2 # inplace of print some popupmenu will be implemented
def conti():
idt=1
while idt==1:
w.config(bg="red") # in place of red it will be a random color
w.update_idletasks()
w= Canvas(root, width=600, height=600)
w.grid(row=0, column=0)
line1 = w.create_line(200,200,300,300, width=10, tags="line1", fill="black")
w.tag_bind(line1, "<Button-3>", lambda e, w2="test1" :do_popup(e,w2))
f = Frame(root)
f.grid(row=0, column=1, sticky=N)
f1=Button(f, text='visual', command=lambda :conti())
f1.grid(row=0, column=1,columnspan=1, sticky=W+N+S+E)
mainloop()
will multiprocessing work?
I am using windows 7 32 bit with python 2.7.3
Thanks in advance
When your script enters the mainloop then the events are executed.
To make reoccurring updates I like to do this:
def conti():
try:
w.config(bg="red") # in place of red it will be a random color
finally:
# start conti after 10 milliseconds,
root.after(10, conti)
# could also be 0ms to handle events
root.after(0, conti)
You can see root.mainloop as
while not (root.quit was called):
root.update()
This way wou can do:
root.quit()
and conti automatically stops.
There is no concurrency as with threads in mainloops.
But you can put a mainloop() somewhere when you create an own dialog box and conti will go on.
If you use the modules tkMessageBox(Python2) or tkinter.messagebox(Python3) then you conti should run while the dialog is open.
Does this answer your questions?
PS: do root.protocol("WM_DELETE_WINDOW", root.quit) to make the mainloop end when you close the window.