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?
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
add a comment |
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
add a comment |
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
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
python canvas turtle-graphics
asked Mar 8 at 1:48
Ben BriddonBen Briddon
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
edited Mar 8 at 7:07
answered Mar 8 at 6:51
cdlanecdlane
19.5k21145
19.5k21145
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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