简体   繁体   中英

Python Execution Order with GUI

I'm having some issues with the following code. This is the first time that I'm working with a GUI and it's been a while since I've worked with python as well. When I try to execute the solfield function with the button, it yields no output.

from Tkinter import *
import math

master = Tk()

n = float()
I = 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 would be appreciated as well, as there will eventually be much more coding for me to do.

This has been solved. If anyone is interested, here is the code after several fixes were 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())
    I = 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 is here:

calculate_button = Button(text='Calculate', command=solfield())

To pass the function solfield itself as the command , just use its name:

calculate_button = Button(text='Calculate', command=solfield)

What you're doing is calling the function, and then passing the return value of that function as the command.

Since you defined solfield above as do-nothing function, that return value is None , so you're telling calculate_button that its command=None , and it's properly doing nothing.


Meanwhile, as SethMMorton pointed out (but then deleted):

You have two functions named solfield , and you are naming a variable solfield in one of your solfield functions. Remove the empty function (the one with pass), and using a different variable name in the remaining function.

This isn't causing your actual problem, but it's certainly adding to the confusion that makes it harder for you to find the problem. (For example, if you hadn't included the excess empty definition of solfield at all, you would have gotten a NameError in the incorrect line, which would have made things easier to debug.)


Putting it all together, what you should do is:

  1. Get rid of the empty ( pass -only) definition of solfield .
  2. Move the real implementation of solfield up above the point where you build the GUI.
  3. Don't name a local variable solfield within the function.
  4. Pass just solfield , not solfield() as the command for calculate_button .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM