How to make a text disappear after certain amount of time? [duplicate] - python

This question already has an answer here:
Displaying Label on Tkinter for a fixed amount of time
(1 answer)
Closed 3 years ago.
I am really new to Python. I have a memory game assignment. I've started doing it but I am kind of stuck right now because I have to generate numbers that are gonna appear on the screen and disappear after 2 seconds. I managed to generate the numbers and I put the numbers as text on a label but can't make them disappear. Is there a function to do it?

For Tkinter there are some options for "forgetting" (making the label disappear).
label.pack_forget()
label.place_forget()
If you used pack then use pack_forget() and if your using place use place_forget(). To make them reappear, use the pack or place function again. To wait a certain amount of time you can use time.sleep but the better option is to use .after()
tk.after(10, function)

Related

How click affects just on above rect in pygame? [duplicate]

This question already has answers here:
Pygame mouse clicking detection
(4 answers)
Closed 7 months ago.
I have lots of rects and each one has its own function when clicking on it.
Some of these rects are overlap sometimes. When I click somewhere, I want to execute just the above rect function (not all rects in that position).
Thanks
without a minimum working example it's difficult to gauge the exact issues but assuming you are rendering all of these rectangles in order I would check for clicks in the reverse order of rendering, that way once a click check passes you are guaranteed to stop checking any of the items that were drawn previous to the one that passed the click check, which presumably would be above.
to stop checking further you can use the break keyword like so:
for rect in my_rects[::-1]: #this loops over your rectangles in reverse
if rect.clicked(): # this is pseudocode, you'll have to write the clicked() check yourself
do clicky stuff here...
break
Give a name/var to each rectangle, then use a for loop and break once the rect is the desired one.

Is there a way to get the state of an object on a Tkinter canvas? [duplicate]

This question already has answers here:
Getting the fill color or any other property of a item drawn in a canvas in tkinter(Python)
(2 answers)
Closed 2 years ago.
I there anyway to check the state of a canvas object? I've dug through the documentation and can't seem to find anything. I am looking for something like:
canvasWidget.getState(node1)
I have a bunch of nodes that I want the user to be able to draw lines that connect the nodes, and I want to have the line follow my cursor to sort of animate the creation of the line like my picture below.
To do so, I have a hidden line that I plan on making visible and then I'll change the coordinates to following the cursor.
The method you are looking for is called itemcget
Syntax is
state = canvas.itemcget(canvasItem,'state')

Good Modules to Display Text on a Screen [duplicate]

This question already has answers here:
pygame - How to display text with font & color?
(7 answers)
How to display some text in pygame?
(1 answer)
Closed 2 years ago.
I am creating a simulation using pygame and have noticed that pygame makes it a little complicated to have text on the window while the application is running. Right now, my program shows a ball moving around the screen and creates a trail of colors identifying how many times it has passed over that area. What I need to happen, and what the text will be used for, is to display the day and temperature above the grid the ball moves around. Every 20 directiontional changes equals a day and each day has a temperature associated with it, so as the program runs, the text has to change respective to the current day and temperature.
My question is:
Are there any good modules that I could import which could do this efficiently, as I only want to use pygame for rendering my simulation in a not so computationally expensive way?

How to save input from a text box to a variable in python? [duplicate]

This question already has an answer here:
How to get text input from user in Pygame? [duplicate]
(1 answer)
Closed 5 years ago.
I'm creating a simple game to try to learn pygame, and I currently have a game where the user controls a car and has to dodge trucks on a highway.
The game starts off pretty slowly, but I want to add an input box for the user to set their own starting speed.
I know how to make working buttons, but I would rather have a text input that saves to a variable as an integer.
Can anyone help with this?
you can use inputbox form pygame.
refer link
Then use:
inputbox.ask(screen, "question")
This will return string which can be casted to int

how to get pygame framerate higher? [duplicate]

This question already has answers here:
Why does my game made with Pygame suddenly lag for a few seconds? [duplicate]
(1 answer)
Pygame is running slow
(1 answer)
Closed 2 years ago.
I'm designing a game in pygame that you can download here: https://github.com/hailfire006/games unfortunately, when you fly the ship near the space station, the framerate drops from a nice 30-40 to 16-20. I believe this is due to my blitting sprites onto the screen every frame, and I have been told that there is a way to only redraw parts of the screen that need redrawing, but no one ever explains how to do this.
Someone please help, I'm beginning to worry that I should abandon hope of ever making a good game in python, and that I'll have to learn an entirely new language just to deal with framerate...
p.s. any other way you know how to raise the fps would be welcome also

Categories