1

I'm wanting to create a simple help window or text in Tkinter so if the user is unsure of what to do, he can simply press the "HELP?" button instead of having to read my comments in the code. However, I'm having trouble actually displaying anything, or rather, displaying the help text when the button is pressed and not before. When I run the code, the button and the label containing the help (currently represented by "Hello") are shown. I don't want the text to be visible until the button is pressed.

There are some similarities between my code and this code, but I still can't get it to work correctly after following the description, so I rewrote my code back to the original.

Here's the code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  Wave1.py
#  
#  Copyright 2015 FRED <fred@matthew24-25>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
# 
# This code calculates the frequency of EM radiation and the energy of 
# one quantum of the radiation. 
# Wavelength should be given in meters/wave.
# Frequency is in Hertz.
# Energy is in either Joules or electron volts.
# Energy is in either Joules or electron volts.

# I want division to be similar to Python 3, and I want to use Tkinter. 
from __future__ import division
import Tkinter as tk

# Runs if buttonJ is selected (calculates energy in joules).
def joules(wavelength, freq_out, energy_out):

    # Calculate the values.
    freq = 299792458 / wavelength
    energy = 6.62606957e-34 * freq

    # Return the values.
    freq_out.set("Frequency: %s Hz" % freq)
    energy_out.set("Energy/quantum: %s Joules" % energy,)


# Runs if buttoneV is selected (calculates energy in electron volts)   
def ev(wavelength, freq_out, energy_out):

    # Caculate the values.
    freq = 299792458 / wavelength
    energy = (6.62606957e-34 * freq) / (1.602E-19)

    # Return the values.
    freq_out.set("Frequency: %s Hz" % freq)
    energy_out.set("Energy/quantum: %s eV" % energy)

# Help function.
def waveHelp(H):

    Htext = "Hello"

    H.set(Htext)


# Define the GUI and the widgets.
def main():
    # Window and title.
    mainwin = tk.Tk()
    mainwin.title("Wave1")
    mainwin.configure(bg='chartreuse2')
    #mainwin.geometry("300x180")

# Entry section.
    tk.Label(mainwin, text="Wavelength:", bg='chartreuse2').pack()

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

# Button section.
    tk.Label(mainwin, text='Options', bg='chartreuse2').pack(pady=5)

    frameButton = tk.Frame(mainwin, bg="chartreuse2")

    buttonJ = tk.Button(frameButton, text="Joules", bg="snow", command=lambda:
                joules(wavelength.get(), freq, energy)).grid(row=0, column=0)

    buttoneV = tk.Button(frameButton, text = "eV", bg="snow", command = lambda:
                ev(wavelength.get(), freq, energy)).grid(row=0, column=1)

    frameButton.pack()

# Answers section.    
    tk.Label(mainwin, text='Returned Values:', bg='chartreuse2').pack(pady=5)

    frameAns = tk.Frame(mainwin, bg='chartreuse2')

    freq = tk.StringVar()
    tk.Label(frameAns, textvariable=freq, fg = 'dodger blue', bg='chartreuse2').grid(row=2, sticky="W")

    energy = tk.StringVar()
    tk.Label(frameAns, textvariable=energy,fg='dodger blue', bg='chartreuse2').grid(row=3, sticky="W")

    frameAns.pack()


# HELP.

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

    HELPbutton = tk.Button(mainwin, text = "HELP?", fg = "firebrick2", command =  waveHelp(guiHelp)).pack()

# Run. [Forest, RUN! :) ]   
    mainwin.mainloop()




if __name__ == "__main__":
    main()
Community
  • 1
  • 1
Fred Barclay
  • 834
  • 1
  • 13
  • 24
  • I figured it out. I hadn't passed "lambda:" to the HELPbutton line in command=. It should have read like this: `HELPbutton = tk.Button(mainwin, text = "HELP?", fg = "firebrick2", command = lambda: waveHelp(guiHelp)).pack(side = tk.LEFT)` OOPS :) – Fred Barclay Apr 21 '15 at 13:47

0 Answers0