user interface - Python Execution Order with GUI -
i'm having issues following code. first time i'm working gui , it's been while since i've worked python well. when try execute solfield function button, yields no output.
from tkinter import * import math master = tk() n = float() = float() def solfield(): pass label_coils = label(text='number of coils per meter', textvariable=n) label_coils.grid() coils = entry(master) coils.grid() label_current = label(text='current in amps', textvariable=i) label_current.grid() current = entry(master) current.grid() calculate_button = button(text='calculate', command=solfield()) calculate_button.grid() label_bfield = label(text='b field in +z direction') label_bfield.grid() label_result = label(text='solfield') label_result.grid() master.title('coil gun simulation') master.mainloop() def solfield(): mu0 = math.pi*4e-7 solfield = mu0*n*i print solfield
any other tips appreciated well, there more coding me do.
this has been solved. if interested, here code after several fixes made:
from tkinter import * import math master = tk() label_coils = label(text='number of coils per meter') label_coils.grid() coils = entry(master) coils.grid() label_current = label(text='current in amps') label_current.grid() current = entry(master) current.grid() def solfield(): mu0 = math.pi*4e-7 n = float(coils.get()) = float(current.get()) fieldmag = mu0*n*i print fieldmag calculate_button = button(text='calculate', command=solfield) calculate_button.grid() label_bfield = label(text='b field in +z direction') label_bfield.grid() label_result = label(text='solfield') label_result.grid() master.title('coil gun simulation') master.mainloop()
the problem here:
calculate_button = button(text='calculate', command=solfield())
to pass function solfield
command
, use name:
calculate_button = button(text='calculate', command=solfield)
what you're doing calling function, , passing return value of function command.
since defined solfield
above do-nothing function, return value none
, you're telling calculate_button
command=none
, , it's doing nothing.
meanwhile, sethmmorton pointed out (but deleted):
you have 2 functions named
solfield
, , naming variablesolfield
in 1 ofsolfield
functions. remove empty function (the 1 pass), , using different variable name in remaining function.
this isn't causing actual problem, it's adding confusion makes harder find problem. (for example, if hadn't included excess empty definition of solfield
@ all, have gotten nameerror
in incorrect line, have made things easier debug.)
putting together, should is:
- get rid of empty (
pass
-only) definition ofsolfield
. - move real implementation of
solfield
above point build gui. - don't name local variable
solfield
within function. - pass
solfield
, notsolfield()
command
calculate_button
.
Comments
Post a Comment