I have a problem: when I add a button to my tkinter application, the command attribute doesn't work properly. I tried many things but it didn't work. can someone help me?
my code:
from tkinter import ttk
import turtle
class App(tk.Tk):
def __init__(self):
super().__init__()
self.width = 1910
self.height = 828
self.geometry(f"{self.width}x{self.height} -10 0")
self.text = tk.Text(self)
self.text.pack(padx=10, pady=10, fill=tk.BOTH, expand=True, side=tk.LEFT)
self.run = ttk.Button(self, text="run", command=self.turtle.arc(-90, 90, 100)) # doesn't work
self.run.pack(fill=tk.X, padx=10, pady=10)
self.canvas = tk.Canvas(self)
self.canvas.config()
self.canvas.pack(side=tk.LEFT, expand=True, padx=10, pady=10, fill=tk.BOTH)
self.screen = turtle.TurtleScreen(self.canvas)
self.pen = turtle.RawTurtle(self.screen)
self.turtle = self.Turtle(self)
def start(self):
self.mainloop()
class Turtle:
def __init__(self, app):
self.pen = app.pen
def arc(self, afrom, ato, r):
self.pen.seth(afrom)
self.pen.up()
self.pen.right(90)
self.pen.forward(r)
self.pen.left(90)
self.pen.down()
self.pen.circle(ato - afrom, r)
self.pen.left(90)
self.pen.forward(r)
self.pen.left(90)
I think the problem is in the following:
self.run = ttk.Button(self, text="run", command=self.turtle.arc(-90, 90, 100)) # doesn't work
thanks in advance!
CodePudding user response:
Try this
# use a lambda to call an anonymous function as the 'command'
self.run = ttk.Button(self, text="run", command=lambda: self.turtle.arc(-90, 90, 100))
And yes, as has been pointed out, you want to use command
not callback