Python - problem with onkeypress() and listen() The Next CEO of Stack OverflowCalling an external command in PythonWhat are metaclasses in Python?Is there a way to run Python on Android?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

How to safely derail a train during transit?

Can a caster that cast Polymorph on themselves stop concentrating at any point even if their Int is low?

How to make a variable always equal to the result of some calculations?

How to be diplomatic in refusing to write code that breaches the privacy of our users

Only print output after finding pattern

Increase performance creating Mandelbrot set in python

How to start emacs in "nothing" mode (`fundamental-mode`)

What happens if you roll doubles 3 times then land on "Go to jail?"

Describing a person. What needs to be mentioned?

Trouble understanding the speech of overseas colleagues

Why does standard notation not preserve intervals (visually)

Horror movie/show or scene where a horse creature opens its mouth really wide and devours a man in a stables

Is it a good idea to use COLUMN AS (left([Another_Column],(4)) instead of LEFT in the select?

WOW air has ceased operation, can I get my tickets refunded?

How to write papers efficiently when English isn't my first language?

How do I construct this japanese bowl?

Is a stroke of luck acceptable after a series of unfavorable events?

What is meant by a M next to a roman numeral?

How to Reset Passwords on Multiple Websites Easily?

How easy is it to start Magic from scratch?

What is the purpose of the Evocation wizard's Potent Cantrip feature?

What can we do to stop prior company from asking us questions?

Any way to transfer all permissions from one role to another?



Python - problem with onkeypress() and listen()



The Next CEO of Stack OverflowCalling an external command in PythonWhat are metaclasses in Python?Is there a way to run Python on Android?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?










1















I have a problem with one function. I use Python 3.7, and when I try to use the onkeypress() function, nothing happens. I try to check it, but the turtle module doesn't react when I press keys.



I try to move my paddle up using the 'w' key. But it doesn't work.
Below are my *.py files:



main.py



import elements
import turtle



#Windows settings

window = turtle.Screen()
window.title("Pong game by Kosa")
window.bgcolor('black')
window.setup(width=800, height=600)
window.tracer(0)

paletka_1 = elements.Objects()
paletka_1.paddle_a()





window.onkeypress(paletka_1.paddle_a_up(), "w")
window.listen()
while True:
window.update()


elements.py



import turtle

class Objects:

def __init__(self):
#Paddle A

#self.paddle_b = turtle.Turtle()


# #Paddle B


#Ball
self.ball = turtle.Turtle()

def paddle_a(self):

paddle_a_x = -350
#paddle_a_y = 0
self.paddle_a = turtle.Turtle()
self.paddle_a.speed(0)
self.paddle_a.shape("square")
self.paddle_a.shapesize(stretch_wid=5, stretch_len=1)
self.paddle_a.color('green')
self.paddle_a.penup()
self.paddle_a.goto(paddle_a_x, 0)

def paddle_b(self):
paddle_b_x = -350
paddle_b_y = 0

self.paddle_b.speed(0)
self.paddle_b.shape("square")
self.paddle_b.shapesize(stretch_wid=5, stretch_len=1)
self.paddle_b.color('green')
self.paddle_b.penup()
self.paddle_b.goto(paddle_b_x, paddle_b_y)

def ball(self):
self.ball.speed(0)
self.ball.shape("square")
self.ball.color('white')
self.ball.penup()
self.ball.goto(0, 0)

def paddle_a_up(self):
y = self.paddle_a.ycor()
y += 20
self.paddle_a.sety(y)
print(y)

def paddle_b_up(self):
y = self.paddle_b.ycor()
y += 20
self.paddle_b.sety(y)


What I get when program starts: I can push keys, but no change in my paddle. Can you find my mistake? I added print(y) in paddle_a_up() just to make sure, that its works. I get the result of print().



It's strange, because there is no error.



enter image description here










share|improve this question




























    1















    I have a problem with one function. I use Python 3.7, and when I try to use the onkeypress() function, nothing happens. I try to check it, but the turtle module doesn't react when I press keys.



    I try to move my paddle up using the 'w' key. But it doesn't work.
    Below are my *.py files:



    main.py



    import elements
    import turtle



    #Windows settings

    window = turtle.Screen()
    window.title("Pong game by Kosa")
    window.bgcolor('black')
    window.setup(width=800, height=600)
    window.tracer(0)

    paletka_1 = elements.Objects()
    paletka_1.paddle_a()





    window.onkeypress(paletka_1.paddle_a_up(), "w")
    window.listen()
    while True:
    window.update()


    elements.py



    import turtle

    class Objects:

    def __init__(self):
    #Paddle A

    #self.paddle_b = turtle.Turtle()


    # #Paddle B


    #Ball
    self.ball = turtle.Turtle()

    def paddle_a(self):

    paddle_a_x = -350
    #paddle_a_y = 0
    self.paddle_a = turtle.Turtle()
    self.paddle_a.speed(0)
    self.paddle_a.shape("square")
    self.paddle_a.shapesize(stretch_wid=5, stretch_len=1)
    self.paddle_a.color('green')
    self.paddle_a.penup()
    self.paddle_a.goto(paddle_a_x, 0)

    def paddle_b(self):
    paddle_b_x = -350
    paddle_b_y = 0

    self.paddle_b.speed(0)
    self.paddle_b.shape("square")
    self.paddle_b.shapesize(stretch_wid=5, stretch_len=1)
    self.paddle_b.color('green')
    self.paddle_b.penup()
    self.paddle_b.goto(paddle_b_x, paddle_b_y)

    def ball(self):
    self.ball.speed(0)
    self.ball.shape("square")
    self.ball.color('white')
    self.ball.penup()
    self.ball.goto(0, 0)

    def paddle_a_up(self):
    y = self.paddle_a.ycor()
    y += 20
    self.paddle_a.sety(y)
    print(y)

    def paddle_b_up(self):
    y = self.paddle_b.ycor()
    y += 20
    self.paddle_b.sety(y)


    What I get when program starts: I can push keys, but no change in my paddle. Can you find my mistake? I added print(y) in paddle_a_up() just to make sure, that its works. I get the result of print().



    It's strange, because there is no error.



    enter image description here










    share|improve this question


























      1












      1








      1








      I have a problem with one function. I use Python 3.7, and when I try to use the onkeypress() function, nothing happens. I try to check it, but the turtle module doesn't react when I press keys.



      I try to move my paddle up using the 'w' key. But it doesn't work.
      Below are my *.py files:



      main.py



      import elements
      import turtle



      #Windows settings

      window = turtle.Screen()
      window.title("Pong game by Kosa")
      window.bgcolor('black')
      window.setup(width=800, height=600)
      window.tracer(0)

      paletka_1 = elements.Objects()
      paletka_1.paddle_a()





      window.onkeypress(paletka_1.paddle_a_up(), "w")
      window.listen()
      while True:
      window.update()


      elements.py



      import turtle

      class Objects:

      def __init__(self):
      #Paddle A

      #self.paddle_b = turtle.Turtle()


      # #Paddle B


      #Ball
      self.ball = turtle.Turtle()

      def paddle_a(self):

      paddle_a_x = -350
      #paddle_a_y = 0
      self.paddle_a = turtle.Turtle()
      self.paddle_a.speed(0)
      self.paddle_a.shape("square")
      self.paddle_a.shapesize(stretch_wid=5, stretch_len=1)
      self.paddle_a.color('green')
      self.paddle_a.penup()
      self.paddle_a.goto(paddle_a_x, 0)

      def paddle_b(self):
      paddle_b_x = -350
      paddle_b_y = 0

      self.paddle_b.speed(0)
      self.paddle_b.shape("square")
      self.paddle_b.shapesize(stretch_wid=5, stretch_len=1)
      self.paddle_b.color('green')
      self.paddle_b.penup()
      self.paddle_b.goto(paddle_b_x, paddle_b_y)

      def ball(self):
      self.ball.speed(0)
      self.ball.shape("square")
      self.ball.color('white')
      self.ball.penup()
      self.ball.goto(0, 0)

      def paddle_a_up(self):
      y = self.paddle_a.ycor()
      y += 20
      self.paddle_a.sety(y)
      print(y)

      def paddle_b_up(self):
      y = self.paddle_b.ycor()
      y += 20
      self.paddle_b.sety(y)


      What I get when program starts: I can push keys, but no change in my paddle. Can you find my mistake? I added print(y) in paddle_a_up() just to make sure, that its works. I get the result of print().



      It's strange, because there is no error.



      enter image description here










      share|improve this question
















      I have a problem with one function. I use Python 3.7, and when I try to use the onkeypress() function, nothing happens. I try to check it, but the turtle module doesn't react when I press keys.



      I try to move my paddle up using the 'w' key. But it doesn't work.
      Below are my *.py files:



      main.py



      import elements
      import turtle



      #Windows settings

      window = turtle.Screen()
      window.title("Pong game by Kosa")
      window.bgcolor('black')
      window.setup(width=800, height=600)
      window.tracer(0)

      paletka_1 = elements.Objects()
      paletka_1.paddle_a()





      window.onkeypress(paletka_1.paddle_a_up(), "w")
      window.listen()
      while True:
      window.update()


      elements.py



      import turtle

      class Objects:

      def __init__(self):
      #Paddle A

      #self.paddle_b = turtle.Turtle()


      # #Paddle B


      #Ball
      self.ball = turtle.Turtle()

      def paddle_a(self):

      paddle_a_x = -350
      #paddle_a_y = 0
      self.paddle_a = turtle.Turtle()
      self.paddle_a.speed(0)
      self.paddle_a.shape("square")
      self.paddle_a.shapesize(stretch_wid=5, stretch_len=1)
      self.paddle_a.color('green')
      self.paddle_a.penup()
      self.paddle_a.goto(paddle_a_x, 0)

      def paddle_b(self):
      paddle_b_x = -350
      paddle_b_y = 0

      self.paddle_b.speed(0)
      self.paddle_b.shape("square")
      self.paddle_b.shapesize(stretch_wid=5, stretch_len=1)
      self.paddle_b.color('green')
      self.paddle_b.penup()
      self.paddle_b.goto(paddle_b_x, paddle_b_y)

      def ball(self):
      self.ball.speed(0)
      self.ball.shape("square")
      self.ball.color('white')
      self.ball.penup()
      self.ball.goto(0, 0)

      def paddle_a_up(self):
      y = self.paddle_a.ycor()
      y += 20
      self.paddle_a.sety(y)
      print(y)

      def paddle_b_up(self):
      y = self.paddle_b.ycor()
      y += 20
      self.paddle_b.sety(y)


      What I get when program starts: I can push keys, but no change in my paddle. Can you find my mistake? I added print(y) in paddle_a_up() just to make sure, that its works. I get the result of print().



      It's strange, because there is no error.



      enter image description here







      python event-handling turtle-graphics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 21 at 16:34









      cdlane

      19.7k21245




      19.7k21245










      asked Mar 20 at 22:00









      IthIth

      183




      183






















          2 Answers
          2






          active

          oldest

          votes


















          1














          This is a common beginner's error when setting event handlers:



          window.onkeypress(paletka_1.paddle_a_up(), "w")


          You don't to call paddle_a_up, you want to pass it on for some other code to call when the event happens:



          window.onkeypress(paletka_1.paddle_a_up, "w")


          Try that and see if it works better for you. As far as the rest of your code goes, I've some suggestions:



          window.tracer(0)


          Avoid tracer() and update() until your program is working otherwise it will just complicate the development and debug process. Only add them back if you need them -- if the program works to your satisfaction, leave them out.



          while True:
          window.update()


          This loop really should instead be a call to mainloop() to turn control over to tkinter's event handler:



          window.mainloop()


          Having member variables and instance methods with the same name is a bad idea:



          self.ball = turtle.Turtle()
          ...
          def ball(self):


          Like the rest of Python, one overwrites the other and bad things happen. My reworked versions of your code:



          main.py



          from turtle import Screen
          import elements

          # Windows settings

          window = Screen()
          window.title("Pong game by Kosa")
          window.bgcolor('black')
          window.setup(width=800, height=600)

          paletka_1 = elements.Objects()

          window.onkeypress(paletka_1.paddle_a_up, "w")

          window.listen()
          window.mainloop()


          elements.py



          from turtle import Turtle

          class Objects:

          def __init__(self):
          # Paddle A
          self.paddle_a = Turtle("square")
          self.init_paddle_a()

          # Paddle B
          self.paddle_b = Turtle("square")
          self.init_paddle_b()

          # Ball
          self.ball = Turtle("square")
          self.init_ball()

          def init_paddle_a(self):

          paddle_a_x = -350

          self.paddle_a.speed('fastest')
          self.paddle_a.shapesize(stretch_wid=5, stretch_len=1)
          self.paddle_a.color('green')
          self.paddle_a.penup()
          self.paddle_a.setx(paddle_a_x)

          def init_paddle_b(self):
          paddle_b_x = 350

          self.paddle_b.speed('fastest')
          self.paddle_b.shapesize(stretch_wid=5, stretch_len=1)
          self.paddle_b.color('red')
          self.paddle_b.penup()
          self.paddle_b.setx(paddle_b_x)

          def init_ball(self):
          self.ball.speed('fastest')
          self.ball.color('white')
          self.ball.penup()
          self.ball.home()

          def paddle_a_up(self):
          y = self.paddle_a.ycor() + 20
          self.paddle_a.sety(y)

          def paddle_b_up(self):
          y = self.paddle_b.ycor() + 20
          self.paddle_b.sety(y)


          This should now put up a window with paddles on the left and right and a ball in the middle. Click on the window and after you can press "w" to make the left paddle rise up. Now finish the program!



          enter image description here






          share|improve this answer
































            1














            Thanks a lot. Admin/moderator thank you for correcting my mistake.



            @cdlane thanku you for reply. Yes, now I can see and understand my mistake. But when you mentioned about mainloop(), correct me if i say something wrong, you suggested to remove x.update() and x.listen() methods, right? Of course T've tried with mainloop but this code doesn't work with mainloop. I can't see in logs event with press button. It looks like the program doesn't 'see' the push button.



            When I replaced mainloop() for the windows.update() and the widnows.listen() everything works fine. Can you check why it doesnt work with mainloop() ?






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55270812%2fpython-problem-with-onkeypress-and-listen%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              This is a common beginner's error when setting event handlers:



              window.onkeypress(paletka_1.paddle_a_up(), "w")


              You don't to call paddle_a_up, you want to pass it on for some other code to call when the event happens:



              window.onkeypress(paletka_1.paddle_a_up, "w")


              Try that and see if it works better for you. As far as the rest of your code goes, I've some suggestions:



              window.tracer(0)


              Avoid tracer() and update() until your program is working otherwise it will just complicate the development and debug process. Only add them back if you need them -- if the program works to your satisfaction, leave them out.



              while True:
              window.update()


              This loop really should instead be a call to mainloop() to turn control over to tkinter's event handler:



              window.mainloop()


              Having member variables and instance methods with the same name is a bad idea:



              self.ball = turtle.Turtle()
              ...
              def ball(self):


              Like the rest of Python, one overwrites the other and bad things happen. My reworked versions of your code:



              main.py



              from turtle import Screen
              import elements

              # Windows settings

              window = Screen()
              window.title("Pong game by Kosa")
              window.bgcolor('black')
              window.setup(width=800, height=600)

              paletka_1 = elements.Objects()

              window.onkeypress(paletka_1.paddle_a_up, "w")

              window.listen()
              window.mainloop()


              elements.py



              from turtle import Turtle

              class Objects:

              def __init__(self):
              # Paddle A
              self.paddle_a = Turtle("square")
              self.init_paddle_a()

              # Paddle B
              self.paddle_b = Turtle("square")
              self.init_paddle_b()

              # Ball
              self.ball = Turtle("square")
              self.init_ball()

              def init_paddle_a(self):

              paddle_a_x = -350

              self.paddle_a.speed('fastest')
              self.paddle_a.shapesize(stretch_wid=5, stretch_len=1)
              self.paddle_a.color('green')
              self.paddle_a.penup()
              self.paddle_a.setx(paddle_a_x)

              def init_paddle_b(self):
              paddle_b_x = 350

              self.paddle_b.speed('fastest')
              self.paddle_b.shapesize(stretch_wid=5, stretch_len=1)
              self.paddle_b.color('red')
              self.paddle_b.penup()
              self.paddle_b.setx(paddle_b_x)

              def init_ball(self):
              self.ball.speed('fastest')
              self.ball.color('white')
              self.ball.penup()
              self.ball.home()

              def paddle_a_up(self):
              y = self.paddle_a.ycor() + 20
              self.paddle_a.sety(y)

              def paddle_b_up(self):
              y = self.paddle_b.ycor() + 20
              self.paddle_b.sety(y)


              This should now put up a window with paddles on the left and right and a ball in the middle. Click on the window and after you can press "w" to make the left paddle rise up. Now finish the program!



              enter image description here






              share|improve this answer





























                1














                This is a common beginner's error when setting event handlers:



                window.onkeypress(paletka_1.paddle_a_up(), "w")


                You don't to call paddle_a_up, you want to pass it on for some other code to call when the event happens:



                window.onkeypress(paletka_1.paddle_a_up, "w")


                Try that and see if it works better for you. As far as the rest of your code goes, I've some suggestions:



                window.tracer(0)


                Avoid tracer() and update() until your program is working otherwise it will just complicate the development and debug process. Only add them back if you need them -- if the program works to your satisfaction, leave them out.



                while True:
                window.update()


                This loop really should instead be a call to mainloop() to turn control over to tkinter's event handler:



                window.mainloop()


                Having member variables and instance methods with the same name is a bad idea:



                self.ball = turtle.Turtle()
                ...
                def ball(self):


                Like the rest of Python, one overwrites the other and bad things happen. My reworked versions of your code:



                main.py



                from turtle import Screen
                import elements

                # Windows settings

                window = Screen()
                window.title("Pong game by Kosa")
                window.bgcolor('black')
                window.setup(width=800, height=600)

                paletka_1 = elements.Objects()

                window.onkeypress(paletka_1.paddle_a_up, "w")

                window.listen()
                window.mainloop()


                elements.py



                from turtle import Turtle

                class Objects:

                def __init__(self):
                # Paddle A
                self.paddle_a = Turtle("square")
                self.init_paddle_a()

                # Paddle B
                self.paddle_b = Turtle("square")
                self.init_paddle_b()

                # Ball
                self.ball = Turtle("square")
                self.init_ball()

                def init_paddle_a(self):

                paddle_a_x = -350

                self.paddle_a.speed('fastest')
                self.paddle_a.shapesize(stretch_wid=5, stretch_len=1)
                self.paddle_a.color('green')
                self.paddle_a.penup()
                self.paddle_a.setx(paddle_a_x)

                def init_paddle_b(self):
                paddle_b_x = 350

                self.paddle_b.speed('fastest')
                self.paddle_b.shapesize(stretch_wid=5, stretch_len=1)
                self.paddle_b.color('red')
                self.paddle_b.penup()
                self.paddle_b.setx(paddle_b_x)

                def init_ball(self):
                self.ball.speed('fastest')
                self.ball.color('white')
                self.ball.penup()
                self.ball.home()

                def paddle_a_up(self):
                y = self.paddle_a.ycor() + 20
                self.paddle_a.sety(y)

                def paddle_b_up(self):
                y = self.paddle_b.ycor() + 20
                self.paddle_b.sety(y)


                This should now put up a window with paddles on the left and right and a ball in the middle. Click on the window and after you can press "w" to make the left paddle rise up. Now finish the program!



                enter image description here






                share|improve this answer



























                  1












                  1








                  1







                  This is a common beginner's error when setting event handlers:



                  window.onkeypress(paletka_1.paddle_a_up(), "w")


                  You don't to call paddle_a_up, you want to pass it on for some other code to call when the event happens:



                  window.onkeypress(paletka_1.paddle_a_up, "w")


                  Try that and see if it works better for you. As far as the rest of your code goes, I've some suggestions:



                  window.tracer(0)


                  Avoid tracer() and update() until your program is working otherwise it will just complicate the development and debug process. Only add them back if you need them -- if the program works to your satisfaction, leave them out.



                  while True:
                  window.update()


                  This loop really should instead be a call to mainloop() to turn control over to tkinter's event handler:



                  window.mainloop()


                  Having member variables and instance methods with the same name is a bad idea:



                  self.ball = turtle.Turtle()
                  ...
                  def ball(self):


                  Like the rest of Python, one overwrites the other and bad things happen. My reworked versions of your code:



                  main.py



                  from turtle import Screen
                  import elements

                  # Windows settings

                  window = Screen()
                  window.title("Pong game by Kosa")
                  window.bgcolor('black')
                  window.setup(width=800, height=600)

                  paletka_1 = elements.Objects()

                  window.onkeypress(paletka_1.paddle_a_up, "w")

                  window.listen()
                  window.mainloop()


                  elements.py



                  from turtle import Turtle

                  class Objects:

                  def __init__(self):
                  # Paddle A
                  self.paddle_a = Turtle("square")
                  self.init_paddle_a()

                  # Paddle B
                  self.paddle_b = Turtle("square")
                  self.init_paddle_b()

                  # Ball
                  self.ball = Turtle("square")
                  self.init_ball()

                  def init_paddle_a(self):

                  paddle_a_x = -350

                  self.paddle_a.speed('fastest')
                  self.paddle_a.shapesize(stretch_wid=5, stretch_len=1)
                  self.paddle_a.color('green')
                  self.paddle_a.penup()
                  self.paddle_a.setx(paddle_a_x)

                  def init_paddle_b(self):
                  paddle_b_x = 350

                  self.paddle_b.speed('fastest')
                  self.paddle_b.shapesize(stretch_wid=5, stretch_len=1)
                  self.paddle_b.color('red')
                  self.paddle_b.penup()
                  self.paddle_b.setx(paddle_b_x)

                  def init_ball(self):
                  self.ball.speed('fastest')
                  self.ball.color('white')
                  self.ball.penup()
                  self.ball.home()

                  def paddle_a_up(self):
                  y = self.paddle_a.ycor() + 20
                  self.paddle_a.sety(y)

                  def paddle_b_up(self):
                  y = self.paddle_b.ycor() + 20
                  self.paddle_b.sety(y)


                  This should now put up a window with paddles on the left and right and a ball in the middle. Click on the window and after you can press "w" to make the left paddle rise up. Now finish the program!



                  enter image description here






                  share|improve this answer















                  This is a common beginner's error when setting event handlers:



                  window.onkeypress(paletka_1.paddle_a_up(), "w")


                  You don't to call paddle_a_up, you want to pass it on for some other code to call when the event happens:



                  window.onkeypress(paletka_1.paddle_a_up, "w")


                  Try that and see if it works better for you. As far as the rest of your code goes, I've some suggestions:



                  window.tracer(0)


                  Avoid tracer() and update() until your program is working otherwise it will just complicate the development and debug process. Only add them back if you need them -- if the program works to your satisfaction, leave them out.



                  while True:
                  window.update()


                  This loop really should instead be a call to mainloop() to turn control over to tkinter's event handler:



                  window.mainloop()


                  Having member variables and instance methods with the same name is a bad idea:



                  self.ball = turtle.Turtle()
                  ...
                  def ball(self):


                  Like the rest of Python, one overwrites the other and bad things happen. My reworked versions of your code:



                  main.py



                  from turtle import Screen
                  import elements

                  # Windows settings

                  window = Screen()
                  window.title("Pong game by Kosa")
                  window.bgcolor('black')
                  window.setup(width=800, height=600)

                  paletka_1 = elements.Objects()

                  window.onkeypress(paletka_1.paddle_a_up, "w")

                  window.listen()
                  window.mainloop()


                  elements.py



                  from turtle import Turtle

                  class Objects:

                  def __init__(self):
                  # Paddle A
                  self.paddle_a = Turtle("square")
                  self.init_paddle_a()

                  # Paddle B
                  self.paddle_b = Turtle("square")
                  self.init_paddle_b()

                  # Ball
                  self.ball = Turtle("square")
                  self.init_ball()

                  def init_paddle_a(self):

                  paddle_a_x = -350

                  self.paddle_a.speed('fastest')
                  self.paddle_a.shapesize(stretch_wid=5, stretch_len=1)
                  self.paddle_a.color('green')
                  self.paddle_a.penup()
                  self.paddle_a.setx(paddle_a_x)

                  def init_paddle_b(self):
                  paddle_b_x = 350

                  self.paddle_b.speed('fastest')
                  self.paddle_b.shapesize(stretch_wid=5, stretch_len=1)
                  self.paddle_b.color('red')
                  self.paddle_b.penup()
                  self.paddle_b.setx(paddle_b_x)

                  def init_ball(self):
                  self.ball.speed('fastest')
                  self.ball.color('white')
                  self.ball.penup()
                  self.ball.home()

                  def paddle_a_up(self):
                  y = self.paddle_a.ycor() + 20
                  self.paddle_a.sety(y)

                  def paddle_b_up(self):
                  y = self.paddle_b.ycor() + 20
                  self.paddle_b.sety(y)


                  This should now put up a window with paddles on the left and right and a ball in the middle. Click on the window and after you can press "w" to make the left paddle rise up. Now finish the program!



                  enter image description here







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 21 at 16:26

























                  answered Mar 21 at 16:03









                  cdlanecdlane

                  19.7k21245




                  19.7k21245























                      1














                      Thanks a lot. Admin/moderator thank you for correcting my mistake.



                      @cdlane thanku you for reply. Yes, now I can see and understand my mistake. But when you mentioned about mainloop(), correct me if i say something wrong, you suggested to remove x.update() and x.listen() methods, right? Of course T've tried with mainloop but this code doesn't work with mainloop. I can't see in logs event with press button. It looks like the program doesn't 'see' the push button.



                      When I replaced mainloop() for the windows.update() and the widnows.listen() everything works fine. Can you check why it doesnt work with mainloop() ?






                      share|improve this answer



























                        1














                        Thanks a lot. Admin/moderator thank you for correcting my mistake.



                        @cdlane thanku you for reply. Yes, now I can see and understand my mistake. But when you mentioned about mainloop(), correct me if i say something wrong, you suggested to remove x.update() and x.listen() methods, right? Of course T've tried with mainloop but this code doesn't work with mainloop. I can't see in logs event with press button. It looks like the program doesn't 'see' the push button.



                        When I replaced mainloop() for the windows.update() and the widnows.listen() everything works fine. Can you check why it doesnt work with mainloop() ?






                        share|improve this answer

























                          1












                          1








                          1







                          Thanks a lot. Admin/moderator thank you for correcting my mistake.



                          @cdlane thanku you for reply. Yes, now I can see and understand my mistake. But when you mentioned about mainloop(), correct me if i say something wrong, you suggested to remove x.update() and x.listen() methods, right? Of course T've tried with mainloop but this code doesn't work with mainloop. I can't see in logs event with press button. It looks like the program doesn't 'see' the push button.



                          When I replaced mainloop() for the windows.update() and the widnows.listen() everything works fine. Can you check why it doesnt work with mainloop() ?






                          share|improve this answer













                          Thanks a lot. Admin/moderator thank you for correcting my mistake.



                          @cdlane thanku you for reply. Yes, now I can see and understand my mistake. But when you mentioned about mainloop(), correct me if i say something wrong, you suggested to remove x.update() and x.listen() methods, right? Of course T've tried with mainloop but this code doesn't work with mainloop. I can't see in logs event with press button. It looks like the program doesn't 'see' the push button.



                          When I replaced mainloop() for the windows.update() and the widnows.listen() everything works fine. Can you check why it doesnt work with mainloop() ?







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 21 at 22:21









                          IthIth

                          183




                          183



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55270812%2fpython-problem-with-onkeypress-and-listen%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현