hi ..... i'm designing a simulator and initially i have to click on the Tkinter interface to place a small oval(dot).That is the initial point where i want the simulation to start.
can anyone please help me or if possible give me the piece of codes


here is my code....
i just want the piece on codes needed to place the oval on the interface.

from Tkinter import *
import Tkinter
from Tkconstants import *
import random


frame=Tkinter.Frame()
frame.pack()

canvas =Tkinter.Canvas(frame,bg = 'white',width=200, height=400)
canvas.pack()

def generate():
for i in range(50):

x1=random.randrange(500)
y1=random.randrange(500)
x2=x1+5
y2= y1+5

canvas.create_oval(x1,y1,x2,y2,fill ="red")

button=Tkinter.Button(frame,fg="blue", text="GENERATE", activebackground='red',font=('verdana', 10, 'bold'),command=generate).pack(padx =50,side = LEFT)

root = Tkinter.Tk()


root.mainloop()


thank in advance

Dani AI

Generated

A few focused points to get a single dot placed where you click and to avoid the common pitfalls in the original post.

First, create the Tk root window before making Frame/Canvas widgets (your first snippet makes the Frame and Canvas before calling Tk). Also match any random ranges to the canvas size — your canvas is 200×400 but the random ranges used 500, which will place dots off-canvas; fixed that in their cleaned example.

To place an oval on a mouse click: bind the left‑mouse click on the canvas, read the click coordinates the event gives (those are relative to the canvas), choose a small radius (for example 2–4 pixels) and compute the oval’s bounding box by subtracting and adding the radius from the click x/y. Create the oval using that bounding box so the click becomes the oval’s center. If the canvas can be scrolled or translated, convert pointer coordinates into canvas coordinates (use the canvas coordinate-conversion methods) before computing the box. ’s suggestion to bind clicks is the right approach.

Quick troubleshooting checklist

  • Make sure you bound the event on the canvas widget (not the parent frame).
  • Ensure mainloop is running and that you created root first.
  • On Python 3 the module name is lowercase tkinter; on Python 2 it’s Tkinter.
  • If initial width/height reads as 1, call an update (or store the constants you used to configure the canvas) before using them.

If you plan to animate the dot later, capture the item id (or give the oval a tag) when you create it; that lets you move or update its coordinates with the canvas move/coords methods. If you want size-by-dragging, bind mouse-drag and mouse-release to provide live feedback and finalize the size.

Recommended Answers

All 4 Replies

Firstly:
To preserve the all important code indentations for Python please use code tags. I will show the needed tags in reverse so they don't activate, at the end of your code add this tag (/code)
at the start of your code add tag (code) or (code=python)

I have somewhat simplified your existing code:

# randomly scatter some dots on a Tkinter canvas

import Tkinter as tk
import random as rn


def generate():
    # generate 50 random dots
    for i in range(50):
        # match with width and height of canvas
        x1 = rn.randrange(200)
        y1 = rn.randrange(400)
        x2 = x1 + 5
        y2 = y1 + 5
        canvas.create_oval(x1, y1, x2, y2, fill ="red")

root = tk.Tk()

frame = tk.Frame()
frame.pack()

canvas = tk.Canvas(frame, bg='white', width=200, height=400)
canvas.pack()

button = tk.Button(frame, fg="blue", text="GENERATE", activebackground='red',
    font=('verdana', 10, 'bold'),command=generate)
button.pack(padx=50, side=tk.LEFT)

root.mainloop()

Hi nish88,

I'm not sure I understand what you mean. Do you want to click on the canvas, then have an oval pop up where you clicked? It sounds like you need to bind mouseclicks on the canvas to a callback function. The way to do that in Tkinter is:

canvas.bind("<Button-1>", click_for_oval)

# Some other code goes here

def click_for_oval(event):
     x, y = event.x, event.y
     # Create the oval on the canvas using x and y as the center

Is this what you had in mind? Bear in mind that you can also bind mouse-dragging and mouse button release to separate callback functions, which would allow you to do things like determine the size of the oval through dragging and releasing - see the for more details.

Hope this helps!

ya its help but i want that when we click by means of mouse not button.
can you please help me...

thank you

Hi nish88,

The string "<Button-1>" is a Tkinter shorthand for "the left mouse button." Incorporate the code I supplied into your Tkinter application and run it, and you will see.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.