i keep getting error "global name v not defined." know has been heavily documented, none of other threads relavant gui situation. anyway, here go:
v_amount= 5000 def set_v_to_something_else(): global v v_amount=v_amount-1000 v.set(v_amount) v = stringvar() v.set(str(v_amount)) #create button allow v label set else vbutton = button(root, text = "change v", command = set_v_to_something_else).pack() vlabel = label(root, textvariable=v).pack() once again, says v not defined, though set equal stringvar()
thanks in advance
perhaps getting error
global name v_amount not defined that because, in code,
v_amount= 5000 def set_v_to_something_else(): global v v_amount=v_amount-1000 v.set(v_amount) when python parses function set_v_to_something_else, marks v_amount local variable because appears on left-hand side of assignment:
v_amount=v_amount-1000 later, when function called, since python regards v_amount local variable, tries evaluate right-hand side of assignment statement first , finds v_amount not defined. (remember, looking in local namespace, not global namespace.)
the fix add global v_amount inside function:
def set_v_to_something_else(): global v_amount v_amount = v_amount-1000 v.set(v_amount) you can remove
global v because not assigning new value v. v.set merely calling method of v.
Comments
Post a Comment