473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

binding - python

Dear All,

I am new with Python. I have question on binding.
I have small pingpong application, I want to change color og the ball
whenever mouse is clicked. I have theBall and changeColor functions. I
don't know how to bind it together. Does anyone know? I am very
appreciate of your help. Thanks.

Here is a source 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='blue' )

def changeColor(sefl, 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 6 '06 #1
4 1464
Quoc,

the following code, verbatim, works for me:

from Tkinter import *

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.20i","0.20i",
fill="red")
self.draw.tag_bind(self.ball, '<Button-1>', self.changeColour)
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()

Cut and paste it into a file and try it out. Make sure you check for
line breaks on long lines.

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

Apr 6 '06 #2
Hi John,

It works! thank you vey much.

I have another question. I would very appreciated if you have any clue.

I want the ball STOP whenever mouse is clicked anywhere, then ball KEEP
MOVING when next mouse click is applied. Thanks.

Regards,
Quoc

Apr 6 '06 #3
On Thu, 2006-04-06 at 16:39 -0700, beta wrote:
Hi John,

It works! thank you vey much.

I have another question. I would very appreciated if you have any clue.

I want the ball STOP whenever mouse is clicked anywhere, then ball KEEP
MOVING when next mouse click is applied. Thanks.


I do have a clue. Simply bind a mouse click to the entire canvas widget
rather than the canvas item.

Add the following line after you create the canvas widget:

self.draw.bind('<Button-1>', self.toggleBallMotion)

where toggleBallMotion stops the ball movement if it is moving and
starts the movement if it is stopped. It is a function of the form:

def toggleBallMotion(self, event):
if self.speed.get() != 0:
self.oldspeed = self.speed.get()
self.speed.set(0)
else:
self.speed.set(self.oldspeed)

You will also need to initialise the self.oldspeed variable when you
first create the Scale widget.

John McMonagle


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

Apr 7 '06 #4
Hi John,

It works. Thank you veyr much.

Cheers,
Q

Apr 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
1
by: Rob Hunter | last post by:
Is there an equivalent to Scheme's LET in Python? LET creates a new binding in the current environment. For example, here's some Scheme code: (let ((x 3)) (let ((f (lambda (arg) (* arg x))))...
1
by: Terry Reedy | last post by:
>From http://film.nvidia.com/page/gelato.html : Gelato is NVIDIA's new professional image rendering software, running on top of its new Quadro FX graphics hardware. In addition to its C++ API, ...
1
by: Riccardo Rossi | last post by:
Hi all! Can anyone tell me what are the binding strategies of this language? How does the binding works in Python? Thanks in advance, Riccardo Rossi.
0
by: Cees Wesseling | last post by:
For a couple of years I am using now my own data binding tool that can create C++ classes from a DTD. At this moment I need to reconsider this tool since I want to use W3C XML Schemas and need to...
1
by: bmcnally | last post by:
I can't tell from the documentation, but I'd like to try and print unicode characters through Python's binding to ncurses. From reading the documentation on the curses module, it doesn't appear...
6
by: Tim Roberts | last post by:
I've been doing COM a long time, but I've just come across a behavior with late binding that surprises me. VB and VBS are not my normal milieux, so I'm hoping someone can point me to a document...
0
by: has | last post by:
Hi all, need a little bit of advice on dynamically binding an embedded Python interpreter. First, the code for anyone that wants a look: ...
7
by: Georg Grabler | last post by:
Hello everybody. There's a C library which i'd like to have python bindings for. I havn't known anything before about how to write python bindings for a C library. I succeeded now by using...
7
by: bukzor | last post by:
I was trying to change the behaviour of print (tee all output to a temp file) by inheriting from file and overwriting sys.stdout, but it looks like print uses C-level stuff to do its writes which...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.