#๐Ÿ”’ Unique variable when running each time

54 messages ยท Page 1 of 1 (latest)

dim chasm
#

In the add_square() function I need something that I can run x times and always have unique variable assigned to it.
Is that possible to do?

round badgerBOT
#

@dim chasm

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.

charred cliff
#

It sounds like you might want a list

#

Every time you create a new square, you can then append it to the list

hollow island
#

but also it would be good to know more about what you're trying to achieve

charred cliff
#

yeah

hollow island
#

sounds a bit xy

#

@dim chasm Can you explain a bit more about what you're trying to do?

#

You also have some "gotcha" errors with how you're using .place

#
btn = tk.Button().place()
#

this actually stores the return of place() in your btn variable

#

and that return is None

dim chasm
hollow island
#

what are you trying to create? Perhaps you might want to use Canvas

dim chasm
#

can I still add the ui stuff to canvas?

hollow island
#

can you give us an idea of what you're trying to create?

#

You've only really told us how you're trying to create

dim chasm
#

A simple engine that can draw some sprites move them around and stuff

#

Just a fun lil project

hollow island
#

and what do the buttons represent?

dim chasm
#

They're just there to add the sprites

hollow island
#

why a button? Why not just an image?

dim chasm
#

wdym

hollow island
#

you can display an image without it being a button

#

That's why I suggested Canvas

dim chasm
#

but I want the buttons

hollow island
#

but why buttons?

dim chasm
#

so I can press on them and add sprites

hollow island
#

but you're using buttons for your sprites

#

which you shouldn't be

dim chasm
#

Where?

hollow island
dim chasm
#

Thats for the button that says "Square"

hollow island
#

ok and when you click the button, where does the sprite get displayed?

dim chasm
#

at 0 0

hollow island
#

as what Widget type?

dim chasm
#

just a normal button

hollow island
#

which goes back to what I've been saying about you shouldn't be displaying a sprite with a button

dim chasm
#

how can I get a click on a specific area without using a button

hollow island
#

you can use the bind method

#

specifically canvas has a way to bind to every single graphic that it displays

#

using tag_bind

dim chasm
#

Why cant I just use buttons?

hollow island
#

because you should use something better suited to the task

#

why not learn the right way to do it?

#

Canvas is literally a widget meant for displaying graphics

#

you're needlessly reinventing a sloppier wheel

dim chasm
#

ok

#

ill try to use canvas

hollow island
#

I'm not one for jumping right to chatGPT for solutions, but it actually provided a nice canvas template to work from here

import tkinter as tk

def on_press(event):
    global prev_x, prev_y, selected_shape
    prev_x = event.x
    prev_y = event.y
    selected_shape = canvas.find_closest(event.x, event.y)

def on_drag(event):
    global prev_x, prev_y
    x, y = event.x, event.y
    canvas.move(selected_shape, x - prev_x, y - prev_y)
    prev_x = x
    prev_y = y

root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=200)
canvas.pack()

# Create some draggable shapes
rectangle = canvas.create_rectangle(50, 50, 100, 100, fill="blue")
oval = canvas.create_oval(150, 50, 200, 100, fill="green")
line = canvas.create_line(250, 50, 300, 100, fill="red")

# Bind mouse events for dragging
canvas.bind("<ButtonPress-1>", on_press)
canvas.bind("<B1-Motion>", on_drag)

root.mainloop()

#

this gives a good idea of what Canvas is used for

#

try running this. You can click and drag the shapes to move them

round badgerBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.