#πŸ”’ Not rising, or going down

44 messages Β· Page 1 of 1 (latest)

neat nest
#

Trying to get the rectangle to shrink and then rise, but if I uncomment the rise_step() call, it rises up, but never comes down again. And if I leave it rise_step() commented out, it shrinks, but doesn't come back up
Here's my code

import tkinter as tk
root = tk.Tk()
c = tk.Canvas(root, width=400, height=400, bg='white')
c.pack()
def draw_step(x=0):
    if x > 801:
        return
    value = x * 0.1
    c.delete("rect")  # clear previous rectangle
    c.create_rectangle(95, 95 + value, 145, 188, fill="red", outline='', tags="rect")
    c.after(10, draw_step, x + 1)  # smaller delay = smoother motion
def rise_step(x=0):
    if x > 801:
        return
    value = x * 0.1
    c.delete("rect")  # clear previous rectangle
    c.create_rectangle(95, 95 - value, 145, 188, fill="red", outline='', tags="rect")
    c.after(10, rise_step, x + 1)  # smaller delay = smoother motion


draw_step()
# rise_step()
root.mainloop()


candid spokeBOT
#

@neat nest

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

karmic mulch
#

!code

candid spokeBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

karmic mulch
#

I don't see in your code why would the rectangle shrink

#

Nor why would it go down

neat nest
karmic mulch
#

Yes but still

#

Isn't the size of your rectangle always 145,188 ?

neat nest
karmic mulch
#

I don't think it does

#

It moves it up

neat nest
karmic mulch
#

Okay so 95 is the left, 95 + value is the width, 145 is the top and 188 is the height ?

neat nest
#

yes

karmic mulch
#

Meh

#

Ok

#

So the top never changes

#

Only the width does

neat nest
karmic mulch
#

Right ok thanks

#

I don't understand why are you using 2 functions at the same time to work with the same rectangle

#

Both function having as only difference the fact that one shrinks and the other extends the rectangle

neat nest
#

one is to make it shrink, while the other one is to make it rise again or increase the height

karmic mulch
#

Okay but they happen at the same time

neat nest
#

yeah i guess so, how to make it happen concurrently

#

and not at the same time

karmic mulch
#

But like, what so you want to achieve ? A boucing effect ?

neat nest
#

shrinking and rising effect

karmic mulch
#

But at the sams time ?

neat nest
#

no one after the other

karmic mulch
#

Wait by rising you mean reducing the y coordinate rigjt ?

neat nest
#

yeah

karmic mulch
#

So the idea would be to do something save the current width and current height in a variable

#

And to update that variable and the rectangle position according to it

#

Until you reach a threshold

neat nest
#

ok

karmic mulch
#

At which you start updating the height

#

Sorry I am on phone it's not practical to write code but the idea is, in pseudocode:

width = ...
Y = ...

function shrink: width -= 1; draw(width,Y); if width > threshold => schedule next shrink else schedule  next rise
function rise: y -= 1; draw(width, Y); schedule raise


#

And the 2 other functions to extend and move the rectangle down

#

Everytome the same pattern

#

Do action on variable, update visual, schedule next function call

candid spokeBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.