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, 6 1588
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.
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
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.
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
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Champaign |
last post by:
Hi all,
I'm working on an educational applet for a child with special needs.
He's got a bit of a trick to make my life more difficult... To interact
with the applet he needs to click on...
|
by: punkin |
last post by:
I am trying to catch mouse position on the entire screen by
dynamically generating mouse click event at every 100 ms. My code
only works for IEs but not any Netscape or Gecko-based browsers. The...
|
by: gsb |
last post by:
I track the mouse location like this code:
function mousePos(e) {
var p = new Object();
if(e) { p.x = e.pageX; p.y = e.pageY; }
else { p.x = event.x; p.y = event.y; }
... (show)
}...
|
by: scott_gui |
last post by:
Creating a Windows application:
<Double-Button-1> mouse event has a conflict when there is also a
binding to the <Button-1> event. It seems like a silly oversight that
performing a double click...
|
by: scott_gui |
last post by:
I am creating a Windows application:
The mouse event <Double-Button-1> has a conflict when the <Button-1>
event also has a binding. Double clicks will first perform the single
click action. This...
|
by: John McMonagle |
last post by:
I tried binding mouse wheel events (<Button-4>, <Button-5>) to a Tkinter
Canvas widget with the hope of using the event.delta value to
subsequently scroll the Canvas.
However, it seems that...
|
by: dfaber |
last post by:
Hi all,
I have been searching for a keyboard and mouse tracker on linux. I've
read solutions (watch at sourceforge) which look at /proc/interrupts to
check keyboard or mouse activity. I also read...
|
by: cb.brite |
last post by:
Hello,
I have tried this using the MouseEnter/MouseLeave events. However these
events do not really refer to the rectangular shape of the form, but
the client area (form area minus children...
|
by: ilikepython |
last post by:
I have a class that I use for a scrolled list. The problem is I want to be able to scroll the list using the mouse wheel. I am using bind and it's not working:
class ScrolledList(Frame):
def...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
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...
| |