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


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

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

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()


thank in advance

10 Years
Discussion Span
Last Post by G-Do

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()

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

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


Edited by mike_2000_17: Fixed formatting


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 Tkinter documentation 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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.