Why is the function called before pressing a button?
Question:
My program draws the fractal using pointSize_max
and pointSize
variables, which are entered by the user in Tkinter. The problem is that the fractal is drawn before pressing a button (as soon as the program is run), and the program does not draw the fractal when the button is pressed.
pointLabel = tk.Label(frame,text="minimaalne pikkus")
pointLabel.pack()
pointSize = tk.StringVar()
pointEntry = tk.Entry(frame,textvariable=pointSize)
pointEntry.pack()
pointSize.set(str(10))
pointLabel_max = tk.Label(frame,text="maksimaalne pikkus")
pointLabel_max.pack()
pointSize_max = tk.StringVar()
pointEntry_max = tk.Entry(frame,textvariable=pointSize_max)
pointEntry_max.pack()
pointSize_max.set(str(30))
drawButton = tk.Button(frame, text = "Draw a fractal", command=koch(int(pointSize_max.get()), int(pointSize.get())))
# koch function draws the fractal
drawButton.pack()
tk.mainloop()
Answers:
The koch
function is called when the button is created, as part of the parameter evaluation before the tk.Button
call. You can create a function object to call instead.
def koch_invoke():
koch(int(pointSize_max.get()), int(pointSize.get()))
drawButton = tk.Button(frame, text = "Draw a fractal", command=koch_invoke)
When your script is compiled it comes to this line and runs the function since you are calling it:
command=koch(int(pointSize_max.get()), int(pointSize.get()))
Try using a lambda to prevent this from happening:
command= lambda x = int(pointSize_max.get()), y = int(pointSize.get()): koch(x, y))
My program draws the fractal using pointSize_max
and pointSize
variables, which are entered by the user in Tkinter. The problem is that the fractal is drawn before pressing a button (as soon as the program is run), and the program does not draw the fractal when the button is pressed.
pointLabel = tk.Label(frame,text="minimaalne pikkus")
pointLabel.pack()
pointSize = tk.StringVar()
pointEntry = tk.Entry(frame,textvariable=pointSize)
pointEntry.pack()
pointSize.set(str(10))
pointLabel_max = tk.Label(frame,text="maksimaalne pikkus")
pointLabel_max.pack()
pointSize_max = tk.StringVar()
pointEntry_max = tk.Entry(frame,textvariable=pointSize_max)
pointEntry_max.pack()
pointSize_max.set(str(30))
drawButton = tk.Button(frame, text = "Draw a fractal", command=koch(int(pointSize_max.get()), int(pointSize.get())))
# koch function draws the fractal
drawButton.pack()
tk.mainloop()
The koch
function is called when the button is created, as part of the parameter evaluation before the tk.Button
call. You can create a function object to call instead.
def koch_invoke():
koch(int(pointSize_max.get()), int(pointSize.get()))
drawButton = tk.Button(frame, text = "Draw a fractal", command=koch_invoke)
When your script is compiled it comes to this line and runs the function since you are calling it:
command=koch(int(pointSize_max.get()), int(pointSize.get()))
Try using a lambda to prevent this from happening:
command= lambda x = int(pointSize_max.get()), y = int(pointSize.get()): koch(x, y))