【发布时间】:2020-09-14 18:44:28
【问题描述】:
我正在 Tkinter 和 turtle 中制作一个应用程序,我想通过数字按键来更改我的 turtle 的宽度。因此,当我输入 2 时,它应该将用户宽度更改为 2,当我输入 1 时,它应该将宽度更改为 1。但是,当我按下键上的数字时,它不起作用?
这里是示例代码:
from tkinter import *
from tkinter import filedialog
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
user.width(1)
# Width Functions
def width_one(event=None):
user.width(1)
def width_two(event=None):
user.width(2)
canvas.focus_set()
canvas.bind('<1>', width_one)
canvas.bind('<2>', width_two)
screen.mainloop()
【问题讨论】:
-
您正在绑定到鼠标按钮 1 和 2 的点击,not 键。使用
'1'和'2'作为数字键。 -
我正在尝试绑定数字键 1 & 2
标签: python tkinter tkinter-canvas python-turtle