my text won't blit to my display in pygame - python

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

Related

How to set two versions of text colours in raspberry Pi using joystick to switch?

from sense_hat import SenseHat
sense=SenseHat()
def black():
sense.clear(0,0,0)
def white():
sense.clear(255,255,255)
sense.stick.direction_left=sense.show_message(text_colour=black,back_colour=white,scroll_speed=0.05)
sense.stick.direction_right=sense.show_message(text_colour=white,back_colour=black,scroll_speed=0.05)
while True:
print ("Hello World")
The system shows: TypeError:show_message() missing 1 required positional argument:text_string
Wondering what I am missing?
sense.stick.direction_left and sense.stick.direction_right are supposed to be assigned functions that will be called when the joystick is pushed either left or right. Your code actually assigns the return value from calling the function sense.show_message(), which is always None.
Regarding the error message that you see from the exception, you are missing the actual message. show_message() requires a positional argument which is the string for the message that you want shown. Try this:
from signal import pause
from sense_hat import SenseHat
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
def pushed_left(event):
sense.show_message('Left', text_colour=BLACK, back_colour=WHITE, scroll_speed=0.05)
def pushed_right(event):
sense.show_message('Right', text_colour=BLACK, back_colour=WHITE, scroll_speed=0.05)
sense = SenseHat()
sense.stick.direction_left = pushed_left
sense.stick.direction_right = pushed_right
pause()
Here the functions to call when the joystick is pushed are assigned properly, and the first argument to show_message() is supplied. Notice that the function objects are not called, they are merely referenced.
Note also that the colour arguments to show_message() should be tuples or lists. Your code assigns a function to these, so it won't work as you might expect. You can create constants with the required values and use these as shown.

How to change label (image form) with next button python

I am currently a novice in python and I'm trying to make a label switch from one image to another by clicking a next button. Here's my code:
from tkinter import *
def next1():
global slide
slide=1
if slide==1:
bglabel.config(image=bg1)
elif slide==2:
bglabel.config(image=bg2)
slide+=1
window.update()
window=Tk()
window.geometry("1500x750+0+0")
bg1=PhotoImage(file="backslide1.png")
bg2=PhotoImage(file="backslide2.png")
nextbutton=PhotoImage(file="next.png")
bglabel=Label(window, image=bg1)
bglabel.place(x=600,y=200)
nextbutton1=Button(window, image=nextbutton, bd=0, command=next1())
window.bind('<Button-1>', next1())
I sat for a good hour or so trying to tamper with the slide variable (trying to declare it before def, removing global, changing value, changing where slide+=1 is, etc) but one of two things always happens; either it's stuck on bg1 with the button clicking but doing nothing, or jumping straight to bg2. I've also tried splitting next1 into two different def's, one for variable tracking, one for switching bglabel, but still the same output. Please help.
(Also, will that window.bind be trouble as I continue to add buttons? If so please let me know how to do it correctly.)
As you mentioned, one 'error' that occurs is that the image immediately jumps to image bg2. This is the line causing that:
nextbutton1=Button(window, image=nextbutton, bd=0, command=next1())
More specifically, where you declare the command associated with the button:
command=next1()
With the enclosed brackets, you're calling the function next1 i.e. as soon as the button is created, run the specified function.
To solve this, just remove the pair of brackets:
nextbutton1=Button(window, image=nextbutton, bd=0, command=next1)
The same goes for your key binding. This way, the button/key now has a reference to the function - it knows what function to run and will run it when the specified action is performed.
More about the key binding...
When you use bind to assign a key to run a function, whatever function that is to be run needs to be made aware as such. Currently, the next function you are trying to bind is given no indication that it can be called using a keyboard button event. To fix that, we set a default parameter in next specifying the event:
def next1(event=None):
#rest of function code here
window.bind('<Button-1>', lambda event: next(event))
Setting a default parameter, event=None, basically means if no value forevent was passed to the function from whatever called it, set it to None by default (in that sense, you can choose to set it to whatever by default). Using lambda for the key bind in this way allows us to pass parameters to functions. We specify what parameter(s) we want to pass to the function and then specify the function, with the parameter(s) enclosed in brackets.
You need to provide the function, not the result of the function. So no parenthesis. Like this:
nextbutton1=Button(window, image=nextbutton, bd=0, command=next1)
Also remove the window.bind line, and your loop logic is broken. "slide" is always 1 since you set that in the function. Are you trying to cycle between the 2 images with every click? If so use itertools.cycle:
from tkinter import *
from itertools import cycle
def next1():
bglabel.config(image=next(bgimages))
window=Tk()
window.geometry("1500x750+0+0")
bg1=PhotoImage(file="backslide1.png")
bg2=PhotoImage(file="backslide2.png")
bgimages = cycle([bg1, bg2])
nextbutton=PhotoImage(file="next.png")
bglabel=Label(window)
bglabel.place(x=600,y=200)
next1() # set the first image
nextbutton1=Button(window, image=nextbutton, bd=0, command=next1)
nextbutton1.pack()
window.mainloop()
(totally untested since i don't have your images).

How to fix 'TypeError: MOVE() takes 0 positional arguments but 1 was given' in Python

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.

unable to use "create_graphics_screen" in pygame?

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.

In python how do i get the size (width,height) of a gtk label?

I have looked all over for this but can't find an answer that works.
element.get_allocation().y returns -1
element.get_allocation().height returns 1
This is the code im using to create the label
item_link_summary = Gtk.Label(item_summary)
item_link_summary.show()
self.layout1.put(item_link_summary, 0, top)
print item_link_summary.get_allocation().y
If you want to know how much space the label requires, you can use the size_request method, which returns a tuple (width, height). If you want to know how much space it is being given, you have to wait until it is realized. This means until it and all of its ancestors, including the toplevel window, are shown. Usually that means after window.show_all() is executed. After that, you can use label.get_allocation().width and label.get_allocation().height.

Categories