#๐ Unique variable when running each time
54 messages ยท Page 1 of 1 (latest)
@dim chasm
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.
It sounds like you might want a list
Every time you create a new square, you can then append it to the list
but also it would be good to know more about what you're trying to achieve
yeah
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
Ok so I have a function add_square() that uses a square image to put into the window. I want to have a variable for every instantiated square to move, rotate, etc...
what are you trying to create? Perhaps you might want to use Canvas
can I still add the ui stuff to canvas?
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
A simple engine that can draw some sprites move them around and stuff
Just a fun lil project
and what do the buttons represent?
They're just there to add the sprites
why a button? Why not just an image?
wdym
but I want the buttons
but why buttons?
so I can press on them and add sprites
Where?
Thats for the button that says "Square"
ok and when you click the button, where does the sprite get displayed?
at 0 0
as what Widget type?
just a normal button
which goes back to what I've been saying about you shouldn't be displaying a sprite with a button
how can I get a click on a specific area without using a button
you can use the bind method
specifically canvas has a way to bind to every single graphic that it displays
using tag_bind
Why cant I just use buttons?
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
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
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.