我正在尝试使用tkinter的按钮在另一个图形窗口中控制乌龟,但是这些按钮不起作用
我试图使用乌龟图形和Tkinter制作一个简单的程序来控制带有按钮的乌龟。但是,似乎只有一个按钮实际上在运行一个功能,即使那样也会产生错误。这是我写的代码:
from tkinter import *
import turtle
global x
global y
x = 1
y = 1
i = 0
root = Tk()
wn = turtle.Screen()
bob = turtle.Turtle()
bob.up()
def goUp(event):
y=y+5
bob.goto(x,y)
def goDown(event):
y=y-5
bob.goto(x,y)
def goRight(event):
x=x+5
bob.goto(x,y)
def goLeft(event):
x=x-5
bob.goto(x,y)
topFrame = Frame(root)
topFrame.pack()
middleFrame = Frame(root)
middleFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack()
button1 = Button(topFrame, text = "Up", fg="red")
button2 = Button(middleFrame, text = "Left", fg="red")
button3 = Button(middleFrame, text = "Right", fg="red")
button4 = Button(bottomFrame, text = "Down", fg="red")
button1.bind("<Button-1>", goUp)
button2.bind("<Button-2>", goLeft)
button3.bind("<Button-3>", goRight)
button4.bind("<Button-4>", goDown)
button1.pack()
button2.pack(side=LEFT)
button3.pack(side=RIGHT)
button4.pack()
root.mainloop()
答案 0 :(得分:1)
TKinter Button
类接受一个command
选项,您可以在其中传递按下按钮时调用的函数。您还需要访问方法内部的全局x
和y
变量。
尝试以下代码:
from tkinter import *
import turtle
global x
global y
x = 1
y = 1
i = 0
root = Tk()
wn = turtle.Screen()
bob = turtle.Turtle()
bob.up()
def goUp():
global x, y
y=y+5
bob.goto(x,y)
def goDown():
global x, y
y=y-5
bob.goto(x,y)
def goRight():
global x, y
x=x+5
bob.goto(x,y)
def goLeft():
global x, y
x=x-5
bob.goto(x,y)
topFrame = Frame(root)
topFrame.pack()
middleFrame = Frame(root)
middleFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack()
button1 = Button(topFrame, text = "Up", fg="red", command=goUp)
button2 = Button(middleFrame, text = "Left", fg="red", command=goLeft)
button3 = Button(middleFrame, text = "Right", fg="red", command=goRight)
button4 = Button(bottomFrame, text = "Down", fg="red", command=goDown)
button1.pack()
button2.pack(side=LEFT)
button3.pack(side=RIGHT)
button4.pack()
root.mainloop()
答案 1 :(得分:0)
当使用turtle.py standalone 时,通常使用Turtle的Turtle
和Screen
类。当在tkinter内 embedded 中使用在tkinter顶部实现的turtle时,我们通常改用RawTurtle
和TurtleScreen
类。您将在the turtle documentation
由于使用不正确,您还应该查看众多online Python global
keyword tutorials之一。幸运的是,我们不需要它来实现此特定程序。
这是对代码的重做,作为一个单一的窗口实现,说明了上述内容:
import tkinter as tk
from turtle import RawTurtle, TurtleScreen
def goUp():
bob.sety(bob.ycor() + 5)
def goDown():
bob.sety(bob.ycor() - 5)
def goRight():
bob.setx(bob.xcor() + 5)
def goLeft():
bob.setx(bob.xcor() - 5)
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack()
wn = TurtleScreen(canvas)
bob = RawTurtle(wn, shape="turtle")
bob.penup()
topFrame = tk.Frame(root)
topFrame.pack()
middleFrame = tk.Frame(root)
middleFrame.pack()
bottomFrame = tk.Frame(root)
bottomFrame.pack()
tk.Button(topFrame, text="Up", fg="red", command=goUp).pack()
tk.Button(middleFrame, text="Left", fg="red", command=goLeft).pack(side=tk.LEFT)
tk.Button(middleFrame, text="Right", fg="red", command=goRight).pack(side=tk.RIGHT)
tk.Button(bottomFrame, text="Down", fg="red", command=goDown).pack()
wn.mainloop()