binding - python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • beta

    #1

    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(s elf):
    self.QUIT = Button(self, text='QUIT', foreground='red ',
    command=self.qu it)
    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=HORIZONT AL, label="ball speed",
    from_=-100, to=100)

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

    # The ball
    #self.ball = self.draw.creat e_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.creat e_oval("0i", "0i", "0.20i",
    "0.20i", fill='blue' )



    def changeColor(sef l, event):
    x = self.draw.canva sx(event.x)
    y = self.draw.canva sy(event.y)
    item = self.draw.find_ closest(x,y)
    currentColour = self.draw.itemc get(item, 'fill')
    if currentColour == 'red':
    self.draw.itemc onfigure(item, fill='blue')
    else:
    self.draw.itemc onfigure(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(sel f)
    self.createWidg ets()
    self.after(10, self.moveBall)


    game = Pong()

    game.mainloop()

    +++++++++++++++
    Thanks,
    Quoc

  • John McMonagle

    #2
    Re: binding - python

    Quoc,

    the following code, verbatim, works for me:

    from Tkinter import *

    class Pong(Frame):
    def createWidgets(s elf):
    self.QUIT = Button(self, text='QUIT', foreground='red ',
    command=self.qu it)
    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=HORIZONT AL, label="ball speed",
    from_=-100, to=100)

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

    # The ball
    self.ball = self.draw.creat e_oval("0i", "0i", "0.20i","0.20i" ,
    fill="red")
    self.draw.tag_b ind(self.ball, '<Button-1>', self.changeColo ur)
    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(se lf, event):
    x = self.draw.canva sx(event.x)
    y = self.draw.canva sy(event.y)
    item = self.draw.find_ closest(x,y)
    currentColour = self.draw.itemc get(item, 'fill')
    if currentColour == 'red':
    self.draw.itemc onfigure(item, fill='blue')
    else:
    self.draw.itemc onfigure(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(sel f)
    self.createWidg ets()
    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.

    Comment

    • beta

      #3
      Re: binding - python

      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

      Comment

      • John McMonagle

        #4
        Re: binding - python

        On Thu, 2006-04-06 at 16:39 -0700, beta wrote:[color=blue]
        > 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.
        >[/color]

        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.toggleBall Motion)

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

        def toggleBallMotio n(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.

        Comment

        • beta

          #5
          Re: binding - python

          Hi John,

          It works. Thank you veyr much.

          Cheers,
          Q

          Comment

          Working...