471,091 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,091 software developers and data experts.

Mouse event - binding

Hello All,

I am new with Python, your help would be very appreciated.

I have a simple pinpong applicaiton. I would like make a ball's color
change when mouse is clicked on it.

Here is my sample code:

from Tkinter import *

import string
class Pong(Frame):
def createWidgets(self):
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)

## The playing field
self.draw = Canvas(self, width="5i", height="5i")

## The speed control for the ball
self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
from_=-100, to=100)

self.speed.pack(side=BOTTOM, fill=X)

# The ball
#self.ball = self.draw.create_oval("0i", "0i", "0.10i",
"0.10i",
# fill="red")
self.theBall()
self.x = 0.05
self.y = 0.05
self.velocity_x = 0.3
self.velocity_y = 0.5

self.draw.pack(side=LEFT)
def theBall(self):
self.ball = self.draw.create_oval("0i", "0i", "0.20i",
"0.20i",
fill="red")

def moveBall(self, *args):
if (self.x > 5.0) or (self.x < 0.0):
self.velocity_x = -1.0 * self.velocity_x
if (self.y > 5.0) or (self.y < 0.0):
self.velocity_y = -1.0 * self.velocity_y

deltax = (self.velocity_x * self.speed.get() / 100.0)
deltay = (self.velocity_y * self.speed.get() / 100.0)
self.x = self.x + deltax
self.y = self.y + deltay

self.draw.move(self.ball, "%ri" % deltax, "%ri" % deltay)
self.after(10, self.moveBall)

def __init__(self, master=None):
Frame.__init__(self, master)
Pack.config(self)
self.createWidgets()
self.after(10, self.moveBall)
game = Pong()

game.mainloop()

Thanks,

Apr 4 '06 #1
6 1528
On Tue, 2006-04-04 at 14:46 -0700, beta wrote:
Hello All,

I am new with Python, your help would be very appreciated.

I have a simple pinpong applicaiton. I would like make a ball's color
change when mouse is clicked on it.

Here is my sample code:

from Tkinter import *

import string
class Pong(Frame):
def createWidgets(self):
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)

## The playing field
self.draw = Canvas(self, width="5i", height="5i")

## The speed control for the ball
self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
from_=-100, to=100)

self.speed.pack(side=BOTTOM, fill=X)

# The ball
#self.ball = self.draw.create_oval("0i", "0i", "0.10i",
"0.10i",
# fill="red")
self.theBall()
self.x = 0.05
self.y = 0.05
self.velocity_x = 0.3
self.velocity_y = 0.5

self.draw.pack(side=LEFT)
def theBall(self):
self.ball = self.draw.create_oval("0i", "0i", "0.20i",
"0.20i",
fill="red")

def moveBall(self, *args):
if (self.x > 5.0) or (self.x < 0.0):
self.velocity_x = -1.0 * self.velocity_x
if (self.y > 5.0) or (self.y < 0.0):
self.velocity_y = -1.0 * self.velocity_y

deltax = (self.velocity_x * self.speed.get() / 100.0)
deltay = (self.velocity_y * self.speed.get() / 100.0)
self.x = self.x + deltax
self.y = self.y + deltay

self.draw.move(self.ball, "%ri" % deltax, "%ri" % deltay)
self.after(10, self.moveBall)

def __init__(self, master=None):
Frame.__init__(self, master)
Pack.config(self)
self.createWidgets()
self.after(10, self.moveBall)
game = Pong()

game.mainloop()

Thanks,

--


You need to bind a button-1 event to the ball tag. After you initially
create the ball item:

self.draw.tag_bind(self.ball, '<Button-1>', changeColour)

where changeColour is a function like so:

def changeColour(self, event):
x = self.draw.canvasx(event.x)
y = self.draw.canvasy(event.y)
item = self.draw.find_closest(x,y)
currentColour = self.draw.itemcget(item, 'fill')
if currentColour == 'red':
self.draw.itemconfigure(item, fill='blue')
else:
self.draw.itemconfigure(item, fill='red')

This will toggle the ball colour between red and blue.

HTH,

John McMonagle

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Apr 4 '06 #2
Dear John,

Thanks for your help. I don't know how to bind the ball only into a
program. Would you mind help me on this? I added the changeColour
function, here is a complete program.

---------------------------
from Tkinter import *
import string

class Pong(Frame):
def createWidgets(self):
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)

## The playing field
self.draw = Canvas(self, width="5i", height="5i")

## The speed control for the ball
self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
from_=-100, to=100)
self.speed.pack(side=BOTTOM, fill=X)

# The ball
self.ball = self.draw.create_oval("0i", "0i", "0.10i", "0.10i",
fill="red")
self.theBall()
self.x = 0.05
self.y = 0.05
self.velocity_x = 0.3
self.velocity_y = 0.5

self.draw.pack(side=LEFT)

def changeColour(self, event):
x = self.draw.canvasx(event.x)
y = self.draw.canvasy(event.y)
item = self.draw.find_closest(x,y)
currentColour = self.draw.itemcget(item, 'fill')
if currentColour == 'red':
self.draw.itemconfigure(item, fill='blue')
else:
self.draw.itemconfigure(item, fill='red')

def moveBall(self, *args):
if (self.x > 5.0) or (self.x < 0.0):
self.velocity_x = -1.0 * self.velocity_x
if (self.y > 5.0) or (self.y < 0.0):
self.velocity_y = -1.0 * self.velocity_y
deltax = (self.velocity_x * self.speed.get() / 100.0)
deltay = (self.velocity_y * self.speed.get() / 100.0)
self.x = self.x + deltax
self.y = self.y + deltay
self.draw.move(self.ball, "%ri" % deltax, "%ri" % deltay)
self.after(10, self.moveBall)

def __init__(self, master=None):
Frame.__init__(self, master)
Pack.config(self)
self.createWidgets()
self.after(10, self.moveBall)

game = Pong()
game.mainloop()

Thanks

Apr 5 '06 #3
On Wed, 2006-04-05 at 09:21 -0700, beta wrote:
Dear John,

Thanks for your help. I don't know how to bind the ball only into a
program. Would you mind help me on this? I added the changeColour
function, here is a complete program.


....SNIP code...
Add the following code after you draw the ball item for the first time:

self.draw.tag_bind(self.ball, '<Button-1>', changeColour)


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Apr 5 '06 #4
Hi John,

It don't work!
I did what you told me, here is theBall function

def theBall(self):
self.ball = self.draw.create_oval("0i", "0i", "0.20i",
"0.20i",
fill="red")
self.draw.tag_bind(self.ball, '<Button-1>', changeColour)

Would you review all my code?

from Tkinter import *
import string

class Pong(Frame):
def createWidgets(self):
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)

## The playing field
self.draw = Canvas(self, width="5i", height="5i")

## The speed control for the ball
self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
from_=-100, to=100)

self.speed.pack(side=BOTTOM, fill=X)

# The ball
#self.ball = self.draw.create_oval("0i", "0i", "0.10i",
"0.10i",
# fill="red")
self.theBall()
self.x = 0.05
self.y = 0.05
self.velocity_x = 0.3
self.velocity_y = 0.5

self.draw.pack(side=LEFT)
def theBall(self):
self.ball = self.draw.create_oval("0i", "0i", "0.20i",
"0.20i",
fill="red")
self.draw.tag_bind(self.ball, '<Button-1>', changeColour)

def changeColour(self, event):
x = self.draw.canvasx(event.x)
y = self.draw.canvasy(event.y)
item = self.draw.find_closest(x,y)
currentColour = self.draw.itemcget(item, 'fill')
if currentColour == 'red':
self.draw.itemconfigure(item, fill='blue')
else:
self.draw.itemconfigure(item, fill='red')

def moveBall(self, *args):
if (self.x > 5.0) or (self.x < 0.0):
self.velocity_x = -1.0 * self.velocity_x
if (self.y > 5.0) or (self.y < 0.0):
self.velocity_y = -1.0 * self.velocity_y

deltax = (self.velocity_x * self.speed.get() / 100.0)
deltay = (self.velocity_y * self.speed.get() / 100.0)
self.x = self.x + deltax
self.y = self.y + deltay

self.draw.move(self.ball, "%ri" % deltax, "%ri" % deltay)
self.after(10, self.moveBall)

def __init__(self, master=None):
Frame.__init__(self, master)
Pack.config(self)
self.createWidgets()
self.after(10, self.moveBall)
game = Pong()

game.mainloop()
Thanks,
Quoc

Apr 5 '06 #5
On Wed, 2006-04-05 at 16:04 -0700, beta wrote:
Hi John,

It don't work!
I did what you told me, here is theBall function

def theBall(self):
self.ball = self.draw.create_oval("0i", "0i", "0.20i",
"0.20i",
fill="red")
self.draw.tag_bind(self.ball, '<Button-1>', changeColour)

Sorry, that should read:

self.draw.tag_bind(self.ball, '<Button-1>', self.changeColour)
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Apr 5 '06 #6
On Wed, 2006-04-05 at 16:04 -0700, beta wrote:
Hi John,

It don't work!
I did what you told me, here is theBall function

def theBall(self):
self.ball = self.draw.create_oval("0i", "0i", "0.20i",
"0.20i",
fill="red")
self.draw.tag_bind(self.ball, '<Button-1>', changeColour)


Sorry, that should read:

self.draw.tag_bind(self.ball, '<Button-1>', self.changeColour)
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Apr 5 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by John Champaign | last post: by
9 posts views Thread by punkin | last post: by
1 post views Thread by scott_gui | last post: by
2 posts views Thread by scott_gui | last post: by
reply views Thread by John McMonagle | last post: by
16 posts views Thread by dfaber | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.