title: Tkinter how to make a calculator function?
body: <p>The global varible f_num is apparently not defined, even though i set global before it in <code>get_value()</code>. In addition <code>value_var</code> and <code>unit_set</code> seems to not be defined aswell. Why? I am trying to make a calculator that does an operation i select from <code>OPTIONS</code> and gives a correct answer based on what unit i want to input into <code>entry_value</code>. The units you choose from are <code>OPTIONS_UNITS</code>. So if i choose "energy" and "nm", the program want me to enter wavelength in nm (i have coded that in), and theoretically spits the correct answer based on my input in to <code>entry_value</code>, the <code>OPTIONS</code> i chose and the <code>OPTIONS_UNITS</code> i chose. To clarify some more: the only part i can't figure out is how to make a function that checks those conditions then if i press <code>button</code>, it computes everything and sends the answer to <code>label</code>.</p>
  
  <pre><code>import tkinter as tk
  from tkinter import *
  from PIL import ImageTk, Image
  
  HEIGHT = 600
  WIDTH = 900
  
  h = 6.63e-34
  c = 3.00e+8
  
  def on_select(value):
      if value == OPTIONS[0]:
          label_enter_what['text'] = 'Enter wavelength in chosen unit below'
          label_unit['text'] = 'Enter what unit \n of wavelength'
          unit_set.set(OPTIONS_UNITS[1])
      elif value == OPTIONS[1]:
          label_enter_what['text'] = 'Enter wavelength in chosen unit below'
          label_unit['text'] = 'Enter what unit \n of wavelength'
          unit_set.set(OPTIONS_UNITS[1])
      elif value == OPTIONS[2]:
          label_enter_what['text'] = 'Enter energy in chosen unit below'
          label_unit['text'] = 'Enter what unit of energy'
          unit_set.set(OPTIONS_UNITS[3])
  
  
  root = tk.Tk()
  
  root.title("Photon property calculator")
  
  canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
  canvas.pack()
  
  background_image = ImageTk.PhotoImage(Image.open('interference.jpg'))
  background_label = tk.Label(root, image=background_image)
  background_label.place(relheight=1, relwidth=1)
  
  frame = tk.Frame(root, bg='#3E3E3E', bd=5)
  frame.place(relx=0.5, rely=0.1, relheight=0.1, relwidth=0.75, anchor='n')
  
  entry_value = tk.Entry(frame, font=40, bg='white')
  entry_value.place(relx=0.175, rely=0, relheight=1, relwidth=0.4)
  
  frame_upper = tk.Frame(root, bg='#3E3E3E', bd=2.5)
  frame_upper.place(relx=0.5, rely=0.03, relheight=0.06, relwidth=0.75, anchor='n')
  
  label_what_to_calc = tk.Label(frame_upper, bg='white', text='Enter what \n to calculate')
  label_what_to_calc.place(relx=0, rely=0, relheight=1, relwidth=0.15)
  
  label_enter_what = tk.Label(frame_upper, bg='white', text='Select an option')
  label_enter_what.place(relx=0.175, relheight=1, relwidth=0.4)
  
  label_unit = tk.Label(frame_upper, bg='white', text='Enter what unit \n of wavelength')
  label_unit.place(relx=0.6, relheight=1, relwidth=0.21)
  
  lower_frame = tk.Frame(root, bg='#60A8FF', bd=10)
  lower_frame.place(relx=0.5, rely=0.3, relheight=0.5, relwidth=0.75, anchor='n')
  
  OPTIONS = [
      "Energy",
      "Frequency",
      "Wavelength"
  ]
  
  value_var = tk.StringVar(value=OPTIONS[0])
  
  drop = tk.OptionMenu(frame, value_var, *OPTIONS, command=on_select)
  drop.place(relx=0, rely=0, relheight=1, relwidth=0.15)
  
  OPTIONS_UNITS = ["μm",
                   "nm",
                   "pm",
                   "aJ",
                   "zJ",
  
  ]
  
  unit_set = tk.StringVar(value=OPTIONS_UNITS[0])
  
  drop_units = tk.OptionMenu(frame, unit_set, *OPTIONS_UNITS)
  drop_units.place(relx=0.6, rely=0, relheight=1, relwidth=0.09)
  
  
  def do_operation(f_num):
      if value_var.get() == OPTIONS_UNITS[0] and unit_set.get() == OPTIONS_UNITS[0]:
          new_num = f_num*10e-28
          return h*c/new_num
  
  
  def get_value():
      first_number = entry_value.get()
      global f_num
      f_num = float(first_number)
      entry_value.delete(0, END)
  
  
  print(do_operation(f_num))
  
  button = tk.Button(frame, text='Calculate!', font=40, bg="#F96612", fg='black', command=lambda: do_operation(f_num))
  button.place(relx=0.7, relheight=1, relwidth=0.3)
  
  label = tk.Label(lower_frame, bg='white', text='1')
  label.place(relheight=1, relwidth=1)
  
  root.mainloop()
  </code></pre>