function - "else" statement executed before "if" statement after `undo` is used in Python -
i have created following function allows user change shape of python turtle image he/she selects file dialog file dialog pops when specific button pressed:
def turtleshape(iop = none): # "iop" supposed image path try: manipulateimage.config(state = normal) flipbutton.config(state = normal) mirrorbutton.config(state = normal) originalbutton.config(state = normal) resetturtle.config(state = normal) rotatebutton.config(state = normal) global klob # following "if-else" statement uses "iop" argument's value value "klob" if `iop` not `none` if iop != none: klob = iop print("lmcv") else: klob = filedialog.askopenfilename() print("klobby") global im im = image.open(klob) pictures.append(im) edited.clear() print(im) im.save(klob + '.gif', "gif") register_shape(klob + '.gif') shape(klob + '.gif') update() except: pass
the above function supposed use iop
argument's value turtle's image if not none
.
now, consider situation; draw bunch of things, set turtle image, , when stamp image, you accidentally press button resets turtle normal shape (yes, button exists in program). oh no! how without going through steps open , edit again? well, undohandler function (shown below) comes in. undoes last function called using many stacks, created deque
s. pretty straightforward if proficient in python:
def undohandler(): if len(function) > 0 , draw.drawing == true: undohandler.handling = true if not hasattr(undohandler, "counter"): undohandler.counter = 0 undohandler.counter += 1 # clear canvas clear() # pop point object function deque function.pop() penup() goto(-200, 100) pendown() try: # execute point before last function called in function: # set canvas , turtle previous state tsd = i.recieveshape() shape(tsd) mndf = i.recieveheading() setheading(mndf) hk = i.getletterheight() global letter_height letter_height = hk rk = i.getletterwidth() global letter_width letter_width = rk milk = i.getspacewidth() global space_width space_width = milk hw = i.getwidth() width(hw) op = i.getcolor() try: color(op) except: g in colors: cp = g.getcolor2() colormode(255) color(cp) # function wrapped in point object , execute j = i.getfunction() j() # following code block issue occurs. basically, if function being run equal `turtleshape`, following... if j.__name__ == "turtleshape": # `hfl` deque holds of `pictures` deque's contents cleared when turtle set default state pictures.extend(hfl) lmcv = pictures.pop() pictures.append(lmcv) try: # resize image previous size if user changes it. otherwise, skip this. bun = picwidth.pop() picwidth.append(bun) mun = picheight.pop() picheight.append(mun) clob = lmcv.resize((int(bun), int(mun)), image.antialias) except: clob = lmcv clob.save(klob + str(undohandler.counter) + ".gif") # use `clob.save` output above source image in `turtleshape` function (this issue occurs) turtleshape(klob + str(undohandler.counter) + ".gif") print("undone!") else: pass except: pass
basically happens here takes function (wrapped in point
object) queue through main functions go through called. functions appended function
deque, after which, when undohandler
called user, screen gets cleared, , latest value popped function
deque other actions except last 1 executed again. issue facing occurs in if j.__name__ == "turtleshape":
code block. basically, reason, when user chooses undo resetting of turtle original shape, works should until turtleshape
function executed undohandler. for reason, when undohandler executes turtleshape
function, when give valid argument iop
attribute of turtleshape
function (as can see in if j.__name__ == "turtleshape":
code block), else
statement executed first (i.e. file dialog comes instead of continuing if
statement). if user clicks cancel
in dialog turtle set previous image.
what wrong in code leading occurrence, , how stop happening? have tried changing klob
attribute in function output saved in undohandler
function to, example, "saveimage"
, still no luck. have tried add if-elif
statement in turtleshape
when supposed choose between iop
or file dialog value klob
, still issue occurs. apparently, executes elif
statement even when not true. therefore, appreciated in remedying issue! :)
it's happening here:
j = i.getfunction() j()
if function you've gotten turtleshape()
function, you're calling once default arguments (i.e., iop = none
). go big if j.__name__ == "turtleshape":
statement , call again inside if
block.
move j()
call else:
block of big if j.__name__ == "turtleshape":
statement, , problem should go away.
does brief explanation make enough sense understand why problem happening? or need me explain bit more in-depth how calling j()
calling turtleshape
parameter iop = none
?
Comments
Post a Comment