1

OK, so first of all, this is my first attempt at a GUI, so please pardon any extra confusion or glaringly obvious mistakes (not to mention plain, flat-out newbiness). :)

I'm trying to create a GUI in Tkinter for Python 2 that takes the wavelength (in m/wave) of EM radiation, and calculates both the frequency and energy of the wave, in Hz and energy-unit/quantum, respectively.

I want to be able to choose the energy-unit by pressing buttons in tk, but for now, I'm starting with Joules.

The flow will look like this: wavelength is passed into entry widget, one of x buttons are pressed to choose what unit to return energy in, and the frequency and energy are then returned.

I've done this much so far

from __future__ import division
import Tkinter as tk
mainwin = tk.Tk()
mainwin.title("Wave1")

#Input label
v = tk.IntVar()
ent = tk.Entry(mainwin, textvariable = v)
ent.pack()

wavelength = v.get()

def Joules(wavelength):
    global wavelength
    freq =  (299792458)/wavelength
    Energy = 6.62606957E-34 * freq
    print freq
    print Energy

b1 = tk.Button(mainwin, text="Joules", command= lambda wavelength: Joules(wavelength))
b1.pack()

mainwin.mainloop()

I'm really not sure where to go from here, so if someone would be so kind as to look this over and make suggestions, I'd appreciate it. Thanks!

Hmm... Looking over this, the code didn't format correctly when I put it in here, so also if someone could explain how to use code tags here... :)

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Fred Barclay
  • 834
  • 1
  • 13
  • 24

1 Answers1

2

First of all, you don't need to make the wavelength global. If you put all your window initialization in a main function, then it quickly gets easier:

def main():
    mainwin = tk.Tk()
    mainwin.title("Wave1")
    # ...   
    mainwin.mainloop()

The normal convention is to call this in this fashion:

if __name__ == "__main__":
    main()

I also set command=lambda: joules(input_label.get()) in the button setup code. The joules function can fetch the wavelength directly form the input field.

When you've structured your code like so, it should be easier to continue adding radio buttons and so on.

In the complete code below, I've made the joules function take additional label inputs, so it can set the result. It may not be optimal code, but should be enough to help you started.

from __future__ import division
import Tkinter as tk

def joules(wavelength, freq_out, energy_out):
    freq = 299792458 / wavelength
    energy = 6.62606957e-34 * freq

    freq_out.set("Frequency: %g" % freq)
    energy_out.set("Energy: %g" % energy)

def main():
    mainwin = tk.Tk()
    mainwin.title("Wave1")

    wavelength = tk.IntVar()
    tk.Entry(mainwin, textvariable=wavelength).pack()

    freq = tk.StringVar()
    tk.Label(mainwin, textvariable=freq).pack()

    energy = tk.StringVar()
    tk.Label(mainwin, textvariable=energy).pack()

    button = tk.Button(mainwin, text="Joules", command=lambda:
                joules(wavelength.get(), freq, energy)).pack()

    mainwin.mainloop()

if __name__ == "__main__":
    main()
Community
  • 1
  • 1
csl
  • 10,937
  • 5
  • 57
  • 89