【问题标题】:Why are there two screens when I only want one?为什么我只想要一个屏幕时却有两个屏幕?
【发布时间】: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


    【解决方案1】:

    删除turtle的事件。只需将其添加到tkinter.canvas中即可。您可以尝试以下代码:

    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(master=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(event=None):
        y = user.ycor()
        y += userspeed
        if y > 280:
            y = 280
        user.sety(y)
        user.setheading(90)  # Changes Direction of the Head
    
    
    def move_down(event=None):
        y = user.ycor()
        y -= userspeed
        if y < -280:
            y = -280
        user.sety(y)
        user.setheading(-90)  # Changes Direction of the Head
    
    
    def move_left(event=None):
        x = user.xcor()
        x -= userspeed
        if x < -280:
            x = - 280
        user.setx(x)
        user.setheading(60)  # Changes Direction of the Head
    
    
    def move_right(event=None):
        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")
    canvas.focus_set()
    canvas.bind("<Up>", move_up)
    canvas.bind("<Down>", move_down)
    canvas.bind("<Left>", move_left)
    canvas.bind("<Right>", move_right)
    screen.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2022-06-22
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多