python - 为什么我只想要一个屏幕时却有两个屏幕?
问题描述
基本上我在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 屏幕上不起作用。我该如何解决?
解决方案
删除海龟的事件。只需将其添加到 .Youtkinter.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()
推荐阅读
- flutter - 初始化形式参数不能在工厂构造函数中使用
- java - 将 Enum 与 switch 语句一起使用:需要常量表达式错误
- android - Fluttter 应用程序在 android studio 模拟器上完美运行,但不会超过物理设备上的加载屏幕
- python - 如何使用 BeautifulSoup 排除表中的某些行?
- git - 当我执行 git status 时,我的 .gitignore 文件显示为未跟踪的文件。这是正常的吗?
- python - 数据集观察不会转换为浮点数
- laravel - 在 Eloquent 结果中获取格式化日期
- reactjs - 如何在路线中传递道具?
- jupyter-notebook - 如何访问 .ipynb 文件?我需要下载 jupyter notebook 才能做到这一点吗?
- dhcp - IP 从静态切换到 DHCP (WMI) 不起作用