按下tkinter按钮时在标签中显示文本

时间:2015-04-21 13:28:52

标签: python tkinter

我想在Tkinter中创建一个简单的帮助窗口或文本,这样如果用户不确定该怎么做,他只需按下" HELP?"按钮而不是必须在代码中阅读我的评论。 但是,我实际上无法显示任何内容,或者更确切地说,按下按钮时显示帮助文本而不是之前显示帮助文本。当我运行代码时,会显示按钮和包含帮助的标签(当前由" Hello"表示)。在按下按钮之前,我不希望文本可见。

我的代码和this 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()

0 个答案:

没有答案