Python - Stop turtle from leaving canvasCalling an external command in PythonWhat are metaclasses in Python?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?How to leave/exit/deactivate a python virtualenv?Does Python have a string 'contains' substring method?

How to cover method return statement in Apex Class?

How do apertures which seem too large to physically fit work?

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?

What happens if you are holding an Iron Flask with a demon inside and walk into an Antimagic Field?

Temporarily disable WLAN internet access for children, but allow it for adults

Does Doodling or Improvising on the Piano Have Any Benefits?

Why would a new[] expression ever invoke a destructor?

Probability that THHT occurs in a sequence of 10 coin tosses

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

Unexpected behavior of the procedure `Area` on the object 'Polygon'

Hero deduces identity of a killer

How do you make your own symbol when Detexify fails?

Biological Blimps: Propulsion

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

creating a ":KeepCursor" command

Angel of Condemnation - Exile creature with second ability

Does the UK parliament need to pass secondary legislation to accept the Article 50 extension

Can I still be respawned if I die by falling off the map?

Why should universal income be universal?

Does malloc reserve more space while allocating memory?

What should you do if you miss a job interview (deliberately)?

How to explain what's wrong with this application of the chain rule?

What are the balance implications behind making invisible things auto-hide?

What are the advantages of simplicial model categories over non-simplicial ones?



Python - Stop turtle from leaving canvas


Calling an external command in PythonWhat are metaclasses in Python?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?How to leave/exit/deactivate a python virtualenv?Does Python have a string 'contains' substring method?













0















I am making a program using tkinter. I have a canvas and a turtle inside of the canvas. Currently, the turtle moves around the screen using the arrow keys, however I want it to not be able to leave the boundaries of the canvas. Here is my current code



canvas = Tk.Canvas(master=self, width=300, height=300)
canvas.grid(row=2, column=2, columnspan=3, padx=50)

canvas.focus_set()

t = turtle.RawTurtle(canvas)
t.setheading(90)

left_bound = -(canvas.winfo_width() / 2)
right_bound = canvas.winfo_width() / 2
top_bound = canvas.winfo_height() / 2
bottom_bound = -(canvas.winfo_height() / 2)

tx = t.xcor()
ty = t.ycor()

if tx > right_bound or tx < left_bound:
t.undo()
if ty > top_bound or ty < bottom_bound:
t.undo()

def move_forward_keys(_):
t.forward(10)

def move_left_keys(_):
t.left(20)

def move_right_keys(_):
t.right(20)

def move_back_keys(_):
t.back(10)

canvas.bind("<Up>", move_forward_keys)
canvas.bind("<Left>", move_left_keys)
canvas.bind("<Right>", move_right_keys)
canvas.bind("<Down>", move_back_keys)


It may be because I am doing something wrong, but this is my code at the moment. Which I think should work because if it leaves, it should undo its last action.



Thanks










share|improve this question


























    0















    I am making a program using tkinter. I have a canvas and a turtle inside of the canvas. Currently, the turtle moves around the screen using the arrow keys, however I want it to not be able to leave the boundaries of the canvas. Here is my current code



    canvas = Tk.Canvas(master=self, width=300, height=300)
    canvas.grid(row=2, column=2, columnspan=3, padx=50)

    canvas.focus_set()

    t = turtle.RawTurtle(canvas)
    t.setheading(90)

    left_bound = -(canvas.winfo_width() / 2)
    right_bound = canvas.winfo_width() / 2
    top_bound = canvas.winfo_height() / 2
    bottom_bound = -(canvas.winfo_height() / 2)

    tx = t.xcor()
    ty = t.ycor()

    if tx > right_bound or tx < left_bound:
    t.undo()
    if ty > top_bound or ty < bottom_bound:
    t.undo()

    def move_forward_keys(_):
    t.forward(10)

    def move_left_keys(_):
    t.left(20)

    def move_right_keys(_):
    t.right(20)

    def move_back_keys(_):
    t.back(10)

    canvas.bind("<Up>", move_forward_keys)
    canvas.bind("<Left>", move_left_keys)
    canvas.bind("<Right>", move_right_keys)
    canvas.bind("<Down>", move_back_keys)


    It may be because I am doing something wrong, but this is my code at the moment. Which I think should work because if it leaves, it should undo its last action.



    Thanks










    share|improve this question
























      0












      0








      0








      I am making a program using tkinter. I have a canvas and a turtle inside of the canvas. Currently, the turtle moves around the screen using the arrow keys, however I want it to not be able to leave the boundaries of the canvas. Here is my current code



      canvas = Tk.Canvas(master=self, width=300, height=300)
      canvas.grid(row=2, column=2, columnspan=3, padx=50)

      canvas.focus_set()

      t = turtle.RawTurtle(canvas)
      t.setheading(90)

      left_bound = -(canvas.winfo_width() / 2)
      right_bound = canvas.winfo_width() / 2
      top_bound = canvas.winfo_height() / 2
      bottom_bound = -(canvas.winfo_height() / 2)

      tx = t.xcor()
      ty = t.ycor()

      if tx > right_bound or tx < left_bound:
      t.undo()
      if ty > top_bound or ty < bottom_bound:
      t.undo()

      def move_forward_keys(_):
      t.forward(10)

      def move_left_keys(_):
      t.left(20)

      def move_right_keys(_):
      t.right(20)

      def move_back_keys(_):
      t.back(10)

      canvas.bind("<Up>", move_forward_keys)
      canvas.bind("<Left>", move_left_keys)
      canvas.bind("<Right>", move_right_keys)
      canvas.bind("<Down>", move_back_keys)


      It may be because I am doing something wrong, but this is my code at the moment. Which I think should work because if it leaves, it should undo its last action.



      Thanks










      share|improve this question














      I am making a program using tkinter. I have a canvas and a turtle inside of the canvas. Currently, the turtle moves around the screen using the arrow keys, however I want it to not be able to leave the boundaries of the canvas. Here is my current code



      canvas = Tk.Canvas(master=self, width=300, height=300)
      canvas.grid(row=2, column=2, columnspan=3, padx=50)

      canvas.focus_set()

      t = turtle.RawTurtle(canvas)
      t.setheading(90)

      left_bound = -(canvas.winfo_width() / 2)
      right_bound = canvas.winfo_width() / 2
      top_bound = canvas.winfo_height() / 2
      bottom_bound = -(canvas.winfo_height() / 2)

      tx = t.xcor()
      ty = t.ycor()

      if tx > right_bound or tx < left_bound:
      t.undo()
      if ty > top_bound or ty < bottom_bound:
      t.undo()

      def move_forward_keys(_):
      t.forward(10)

      def move_left_keys(_):
      t.left(20)

      def move_right_keys(_):
      t.right(20)

      def move_back_keys(_):
      t.back(10)

      canvas.bind("<Up>", move_forward_keys)
      canvas.bind("<Left>", move_left_keys)
      canvas.bind("<Right>", move_right_keys)
      canvas.bind("<Down>", move_back_keys)


      It may be because I am doing something wrong, but this is my code at the moment. Which I think should work because if it leaves, it should undo its last action.



      Thanks







      python canvas turtle-graphics






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 1:48









      Ben BriddonBen Briddon

      83




      83






















          1 Answer
          1






          active

          oldest

          votes


















          0














          The problem is that this code:



          tx = t.xcor()
          ty = t.ycor()

          if tx > right_bound or tx < left_bound:
          t.undo()
          if ty > top_bound or ty < bottom_bound:
          t.undo()


          is at the top level of your file when it should be embedded in the move_forward_keys() and move_back_keys() functions:



          CURSOR_OFFSET = 6 # cursor hotspot closer to front than back

          canvas = Tk.Canvas(master=self, width=300, height=300)
          canvas.grid(row=2, column=2, columnspan=3, padx=50)

          canvas.focus_set()

          t = turtle.RawTurtle(canvas)
          t.setheading(90)

          left_bound = -(canvas.winfo_width() / 2)
          right_bound = canvas.winfo_width() / 2
          top_bound = canvas.winfo_height() / 2
          bottom_bound = -(canvas.winfo_height() / 2)

          def move_forward_keys(_):
          t.forward(9)
          tx, ty = t.position()

          if tx > right_bound or tx < left_bound:
          t.undo()

          if ty > top_bound or ty < bottom_bound:
          t.undo()

          def move_left_keys(_):
          t.left(20)

          def move_right_keys(_):
          t.right(20)

          def move_back_keys(_):
          t.back(9)
          tx, ty = t.position()

          if tx > right_bound - CURSOR_OFFSET or tx < CURSOR_OFFSET + left_bound:
          t.undo()

          if ty > top_bound - CURSOR_OFFSET or ty < CURSOR_OFFSET + bottom_bound:
          t.undo()

          canvas.bind("<Up>", move_forward_keys)
          canvas.bind("<Left>", move_left_keys)
          canvas.bind("<Right>", move_right_keys)
          canvas.bind("<Down>", move_back_keys)





          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%2f55055596%2fpython-stop-turtle-from-leaving-canvas%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The problem is that this code:



            tx = t.xcor()
            ty = t.ycor()

            if tx > right_bound or tx < left_bound:
            t.undo()
            if ty > top_bound or ty < bottom_bound:
            t.undo()


            is at the top level of your file when it should be embedded in the move_forward_keys() and move_back_keys() functions:



            CURSOR_OFFSET = 6 # cursor hotspot closer to front than back

            canvas = Tk.Canvas(master=self, width=300, height=300)
            canvas.grid(row=2, column=2, columnspan=3, padx=50)

            canvas.focus_set()

            t = turtle.RawTurtle(canvas)
            t.setheading(90)

            left_bound = -(canvas.winfo_width() / 2)
            right_bound = canvas.winfo_width() / 2
            top_bound = canvas.winfo_height() / 2
            bottom_bound = -(canvas.winfo_height() / 2)

            def move_forward_keys(_):
            t.forward(9)
            tx, ty = t.position()

            if tx > right_bound or tx < left_bound:
            t.undo()

            if ty > top_bound or ty < bottom_bound:
            t.undo()

            def move_left_keys(_):
            t.left(20)

            def move_right_keys(_):
            t.right(20)

            def move_back_keys(_):
            t.back(9)
            tx, ty = t.position()

            if tx > right_bound - CURSOR_OFFSET or tx < CURSOR_OFFSET + left_bound:
            t.undo()

            if ty > top_bound - CURSOR_OFFSET or ty < CURSOR_OFFSET + bottom_bound:
            t.undo()

            canvas.bind("<Up>", move_forward_keys)
            canvas.bind("<Left>", move_left_keys)
            canvas.bind("<Right>", move_right_keys)
            canvas.bind("<Down>", move_back_keys)





            share|improve this answer





























              0














              The problem is that this code:



              tx = t.xcor()
              ty = t.ycor()

              if tx > right_bound or tx < left_bound:
              t.undo()
              if ty > top_bound or ty < bottom_bound:
              t.undo()


              is at the top level of your file when it should be embedded in the move_forward_keys() and move_back_keys() functions:



              CURSOR_OFFSET = 6 # cursor hotspot closer to front than back

              canvas = Tk.Canvas(master=self, width=300, height=300)
              canvas.grid(row=2, column=2, columnspan=3, padx=50)

              canvas.focus_set()

              t = turtle.RawTurtle(canvas)
              t.setheading(90)

              left_bound = -(canvas.winfo_width() / 2)
              right_bound = canvas.winfo_width() / 2
              top_bound = canvas.winfo_height() / 2
              bottom_bound = -(canvas.winfo_height() / 2)

              def move_forward_keys(_):
              t.forward(9)
              tx, ty = t.position()

              if tx > right_bound or tx < left_bound:
              t.undo()

              if ty > top_bound or ty < bottom_bound:
              t.undo()

              def move_left_keys(_):
              t.left(20)

              def move_right_keys(_):
              t.right(20)

              def move_back_keys(_):
              t.back(9)
              tx, ty = t.position()

              if tx > right_bound - CURSOR_OFFSET or tx < CURSOR_OFFSET + left_bound:
              t.undo()

              if ty > top_bound - CURSOR_OFFSET or ty < CURSOR_OFFSET + bottom_bound:
              t.undo()

              canvas.bind("<Up>", move_forward_keys)
              canvas.bind("<Left>", move_left_keys)
              canvas.bind("<Right>", move_right_keys)
              canvas.bind("<Down>", move_back_keys)





              share|improve this answer



























                0












                0








                0







                The problem is that this code:



                tx = t.xcor()
                ty = t.ycor()

                if tx > right_bound or tx < left_bound:
                t.undo()
                if ty > top_bound or ty < bottom_bound:
                t.undo()


                is at the top level of your file when it should be embedded in the move_forward_keys() and move_back_keys() functions:



                CURSOR_OFFSET = 6 # cursor hotspot closer to front than back

                canvas = Tk.Canvas(master=self, width=300, height=300)
                canvas.grid(row=2, column=2, columnspan=3, padx=50)

                canvas.focus_set()

                t = turtle.RawTurtle(canvas)
                t.setheading(90)

                left_bound = -(canvas.winfo_width() / 2)
                right_bound = canvas.winfo_width() / 2
                top_bound = canvas.winfo_height() / 2
                bottom_bound = -(canvas.winfo_height() / 2)

                def move_forward_keys(_):
                t.forward(9)
                tx, ty = t.position()

                if tx > right_bound or tx < left_bound:
                t.undo()

                if ty > top_bound or ty < bottom_bound:
                t.undo()

                def move_left_keys(_):
                t.left(20)

                def move_right_keys(_):
                t.right(20)

                def move_back_keys(_):
                t.back(9)
                tx, ty = t.position()

                if tx > right_bound - CURSOR_OFFSET or tx < CURSOR_OFFSET + left_bound:
                t.undo()

                if ty > top_bound - CURSOR_OFFSET or ty < CURSOR_OFFSET + bottom_bound:
                t.undo()

                canvas.bind("<Up>", move_forward_keys)
                canvas.bind("<Left>", move_left_keys)
                canvas.bind("<Right>", move_right_keys)
                canvas.bind("<Down>", move_back_keys)





                share|improve this answer















                The problem is that this code:



                tx = t.xcor()
                ty = t.ycor()

                if tx > right_bound or tx < left_bound:
                t.undo()
                if ty > top_bound or ty < bottom_bound:
                t.undo()


                is at the top level of your file when it should be embedded in the move_forward_keys() and move_back_keys() functions:



                CURSOR_OFFSET = 6 # cursor hotspot closer to front than back

                canvas = Tk.Canvas(master=self, width=300, height=300)
                canvas.grid(row=2, column=2, columnspan=3, padx=50)

                canvas.focus_set()

                t = turtle.RawTurtle(canvas)
                t.setheading(90)

                left_bound = -(canvas.winfo_width() / 2)
                right_bound = canvas.winfo_width() / 2
                top_bound = canvas.winfo_height() / 2
                bottom_bound = -(canvas.winfo_height() / 2)

                def move_forward_keys(_):
                t.forward(9)
                tx, ty = t.position()

                if tx > right_bound or tx < left_bound:
                t.undo()

                if ty > top_bound or ty < bottom_bound:
                t.undo()

                def move_left_keys(_):
                t.left(20)

                def move_right_keys(_):
                t.right(20)

                def move_back_keys(_):
                t.back(9)
                tx, ty = t.position()

                if tx > right_bound - CURSOR_OFFSET or tx < CURSOR_OFFSET + left_bound:
                t.undo()

                if ty > top_bound - CURSOR_OFFSET or ty < CURSOR_OFFSET + bottom_bound:
                t.undo()

                canvas.bind("<Up>", move_forward_keys)
                canvas.bind("<Left>", move_left_keys)
                canvas.bind("<Right>", move_right_keys)
                canvas.bind("<Down>", move_back_keys)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 8 at 7:07

























                answered Mar 8 at 6:51









                cdlanecdlane

                19.5k21145




                19.5k21145





























                    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%2f55055596%2fpython-stop-turtle-from-leaving-canvas%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

                    How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                    List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229