user interface - Why is my calculator program not working - Python? -



user interface - Why is my calculator program not working - Python? -

i'm writing programme calculator i'm experiencing little problem. whenever press 1 of buttons, increases numbers 9 every time, though should (from loop). please can tell me why 9 please?

code -

import tkinter plus = true numbers = 0 def main(): def numbutton(i): global numbers if plus == false: numbers-=i else: numbers+=i def quithandler(): root.destroy() def entryhandler(): global numbers numbers+=int(text.get()) text.set("") def printhandler(): text2.set(numbers) def restarthandler(): global numbers root.destroy() plus = true numbers = 0 main() def plushandler(): global plus plus = true def minushandler(): global plus plus = false root = tkinter.tk() frame = tkinter.frame(root).pack(side=tkinter.top) text = tkinter.intvar() text2 = tkinter.intvar() text.set("") text2.set("") tkinter.entry(frame,bd =8,textvariable=text).pack() tkinter.button(frame,padx=8,pady=8,bd=8,text="enter",command=entryhandler).pack() tkinter.button(frame,padx=8,pady=8,bd=8,text="quit",command=quithandler).pack(side=tkinter.right) tkinter.button(frame,padx=8,pady=8,bd=8,text="restart",command=restarthandler).pack(side=tkinter.right) tkinter.button(frame,padx=8,pady=8,bd=8,text="print",command=printhandler).pack(side=tkinter.left) tkinter.entry(frame,bd =8,textvariable=text2).pack(side=tkinter.left) _padx = 16 _pady = 16 _bd = 8 in range (1,10): tkinter.button(frame, padx = _padx, pady = _pady, bd = _bd, text = str(i), command = lambda: numbutton(i)).pack(side = tkinter.left) tkinter.button(frame,padx=8,pady=8,bd=8,text="+",command=plushandler).pack(side=tkinter.left) tkinter.button(frame,padx=8,pady=8,bd=8,text="-",command=minushandler).pack(side=tkinter.left) main()

could tell me how set of within code thing on site, cant figure out , ways site's help shows me isn't working ( or admin prepare please ).

ok guys, someone's emailed me solution:

tkinter.button(frame, padx = _padx, pady = _pady, bd = _bd, text = str(i), command = lambda i=i: numbutton(i)).pack(side = tkinter.left)

had add together i=i after lamda. can explain me i=i thing please?

thanks

command = lambda: numbutton(i)

this doesn't value of i @ time of lambda's creation , insert function. when lambda called, then looks i. i 9 time.

there several ways around problem, focused on ensuring i looked @ command's creation time instead of execution time. 1 i'd utilize functools.partial, tool designed associate function arguments:

from functools import partial ... command=partial(numbutton, i)

you can utilize default argument, kind of kludgy:

command=lambda i=i: numbutton(i)

or write mill function:

def closure_maker(i): def closure(): numbutton(i) homecoming closure ... command=closure_maker(i)

python user-interface python-3.x tkinter calculator

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -