I am following a tutorial on pygame. here is the link->tutorial
On trying the code from tutorial I am getting this error:
NameError: name 'create_graphics_screen' is not defined
But in the link(tutorial) this is the line of code is given. Am I doing something wrong?
import pygame
import sys
background = ["blue50x50.png","green50x50","pink50*50","red50*50","skin50*50","skyblue50*50"]
screen = create_graphics_screen() # this line generates error.
Does "create_graphics_screen" function actually exist in pygame?
If yes do I need to import something to run it?
No, create_graphics_screen() does not exist; replace it pygame.display.set_mode(WIDTH, HEIGHT).
This function will create a display Surface. The arguments passed in are requests for a display type. The actual created display will be the best possible match supported by the system.
The resolution argument is a pair of numbers representing the width and height. The flags argument is a collection of additional options. The depth argument represents the number of bits to use for color.
Related
i am trying to make a score in the top left corner of my screen but it is returning an error.
i have searched it up online and followed the exact steps but still it returns an error.
def score(rounds):
font = pygame.font.SysFont(None, 25)
text = font.render(f'ROUND {rounds}', True, size=25)
game_display.blit(text, (0,0))
render() takes no keyword arguments
i have tried putting the True in as False but that didn't work.
btw what does the middle argument True do?
When you see the following error:
render() takes no keyword arguments
it means, well, that the render function does not take keyword arguments.
Look at your code:
text = font.render(f'ROUND {rounds}', True, size=25)
You call render with a keyword argument.
Just don't do it. Don't use a keyword argument. It's as simple as that.
Also, the third parameter has to be a color-like object, so your code should look like this:
text = font.render(f'ROUND {rounds}', True, pygame.Color('orange'))
Some more notes:
render takes an optional 4th argument (a background color). The documentation of pygame wrongly shows it as keyword argument.
it's better to load your fonts once. Currently, you load the font everytime you call
the score function
instead of the font module, consider using the freetype module, which can to everything the font module can, and much more
Trying to make a simple drawing program based on x an y coordinates an i'm using a function to draw and modify the coordinates in one call without actually giving valuea to the function itself using global variables for what needs modification by the function but it still seees as if i've given it actual direct input.
In a previous version i got away by using a class to memorize the ghanging coordinates and functions of the respective class to do the drawing, but in this case the input method is slightly different since i'm using the scale widget isntead of the buttons and as i mentioned before i did try using global variables in both programs actually and it doesn't work in either of them.
from tkinter import *
win=Tk()
win.title("Etch a Schetch")
win.configure(background="grey")
win.configure(bd=5)
global xa
xa=0
global ya
ya=0
def MOVE():
tabla.create_line(xa,ya,sx.get(),sy.get())
xa=sx.get()
ya=sy.get()
tabla=Canvas(win,width=300,height=300)
sx=Scale(win,from_=0,to=300,length=300,orient="horizontal",command=MOVE)
sy=Scale(win,from_=0,to=300,length=300,command=MOVE)
ex=Button(win,text="Exit",command=exit)
tabla.grid(row=1,column=1)
sx.grid(row=2,column=1)
sy.grid(row=1,column=2)
ex.grid(row=2,column=2)
win.mainloop()
If it would work it would be kinda like an etch a sketch.
I actually just realized what the problem was, to quote mkiever who commented first but i didn't understand untill i did some individuall testing on the interaction between "Scale" and the command that is calling. To put it short and easier to understand the function that is beeing called by "Scale" as its command automaticly takes the value of the scale as an input to the function, as a rezult i only need one declared variable in the paranthesis when i define the function but no paranthesis or input variable is required to give an input to it from the scale.
EXAMPLE:
from tkinter import *
import time
win=Tk()
def P(a):
print(a)
sx=Scale(win,from_=0,to=300,length=300,orient="horizontal",command=P)
sx.pack()
win.mainloop()
Some alteration to the code is still required but it'll be much easier without that pesky error showing up.
Thank you everyone for your advice.
# register shape
turt = turtle.Turtle()
turt.register_shape('player.png')
# player
p = turtle.Turtle()
p.speed(0)
p.color('magenta')
p.shape('player.png')
p.shapesize(2)
p.setheading(0)
p.penup()
p.setposition(-700, 0)
hp = 3
then i get this error: 'Turtle' object has no attribute 'register_shape'
register_shape is not a method on Turtle objects, it's a global function.
So, instead of this:
turt.register_shape('player.png')
… do this:
turtle.register_shape('player.png')
Also, notice that you don't have any use for that turt turtle. Your app only wants to display a single turtle, p, so don't create any others.
Finally, even after you fix this:
At least according to the docs, only GIF images are supported, but you're trying to use a PNG image. The docs may be wrong about that, but there's a good chance they're right, and this is going to fail.
If so, the only way to fix it is to use some other program (it could be one you write yourself in 4 lines of Pillow code, or it could be something like MSPaint or Preview, or a command-line tool like ImageMagick convert) to make a GIF image out of your PNG image.
I'm quite new to Python and have been unsuccessful in finding a way around this problem. I have a GUI using TKinter that displays an image using Label. I would like the user to be able to click on two places in the image and use those two pixel locations elsewhere.
Below is the basic code I'm using so far, but I'm unable to return the pixel locations. I believe bind is not what I want to use, is there another option?
px = []
py = []
def onmouse(event):
px.append(event.x)
py.append(event.y)
return px,py
self.ImgPanel.bind('<button-1>',onmouse)
If I try to use:
px,py = self.ImgPanel.bind('<button-1>',onmouse)
I get an error "Too many values to unpack"
bind is what you want, if you want to capture the x,y coordinate of the click. However, functions called from bindings don't "return". Technically they do, but they return a value to the internals of Tkinter.
What you need to do is set an instance or global variable within the bound function. In the code you included in your question, if you add global px,py, you can then use those values in other code.
I feel like this should be pretty simple, but I guess I am missing something.
So I want to set the icon of a window with one of the stock images. I have tried:
windowIcon = gtk.image_new_form_stock(gtk.STOCK_DIALOG_AUTHENTICATION, gtk.ICON_SIZE_MENU)
window.set_icon(windowIcon.get_pixbuf())
Python then complains that:
File "./sample.py", line 44, in init
window.set_icon(windowIcon.get_pixbuf())
ValueError: image should be a GdkPixbuf or empty
I try to convert the gtkImage to a GdkPixbuf because when I didn't python complained that
TypeError: icon should be a GdkPixbuf or None
In the pygtk documentation it says:
If the storage type of the image is not either gtk.IMAGE_EMPTY or gtk.IMAGE_PIXBUF the ValueError exception will be raised.
So I guessing that storage type of the stock image is wrong. The question is how to I get around this?
You can use the .render_icon() method on any GtkWidget to provide a stock item as the icon.
windowicon = window.render_icon(gtk.STOCK_DIALOG_AUTHENTICATION, gtk.ICON_SIZE_MENU)
window.set_icon(windowicon)
The render_icon() method works as given above. Your code was having problems, because the get_pixbuf() method was returning None. This is because the image was not created from a pixbuf, and for some reason, it won't convert it. Another way of getting a pixbuf from an image is to use the gtk.gdk.pixbuf_new_from_image() function.