【发布时间】:2020-09-14 00:21:20
【问题描述】:
基本上我在turtle中做了一个应用程序,我想将turtle应用程序的内容放到Tkinter画布上。但是,当我运行代码时,我只需要一个屏幕就会创建两个屏幕。
这里是示例代码:
from tkinter import *
import turtle
import time
# Screen
screen = Tk()
screen.geometry("{0}x{1}+0+0".format(screen.winfo_screenwidth(), screen.winfo_screenheight()))
screen.title("Example Code")
screen.configure(bg="Gray")
# Canvas
canvas = Canvas(screen, width="666", height="666")
canvas.place(relx=0.5, rely=0.5, anchor=CENTER)
# Making The User
user = turtle.RawTurtle(canvas)
user.shape("triangle")
user.setheading(90)
user.speed(0)
user.color("black")
user.down()
user.goto(0, 0)
userspeed = 15
# Moving Functions
def move_up():
y = user.ycor()
y += userspeed
if y > 280:
y = 280
user.sety(y)
user.setheading(90) # Changes Direction of the Head
def move_down():
y = user.ycor()
y -= userspeed
if y < -280:
y = -280
user.sety(y)
user.setheading(-90) # Changes Direction of the Head
def move_left():
x = user.xcor()
x -= userspeed
if x < -280:
x = - 280
user.setx(x)
user.setheading(60) # Changes Direction of the Head
def move_right():
x = user.xcor()
x += userspeed
if x > 280:
x = 280
user.setx(x)
user.setheading(0) # Changes Direction of the Head
# Keyboard Bindings For Moving Functions
turtle.listen()
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
screen.mainloop()
当我运行它时,会创建两个屏幕(一个 Tkinter 屏幕和一个海龟屏幕),我只想要 Tkinter 屏幕。但是,当我关闭海龟屏幕时,键盘绑定在 Tkinter 屏幕上不起作用。我该如何解决这个问题?
【问题讨论】:
标签: python python-3.x tkinter turtle-graphics python-turtle