Last active
December 6, 2016 18:32
-
-
Save s2t2/c4ea0d26a89449bad7d4f85a0c6954a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# resources: | |
# + https://www.tutorialspoint.com/python/python_gui_programming.htm | |
# + https://www.tutorialspoint.com/python/tk_radiobutton.htm | |
# + https://www.tutorialspoint.com/python/tk_entry.htm | |
from Tkinter import * | |
window = Tk() | |
# | |
# define variables to hold user input values: | |
# | |
cents_per_minute = IntVar() | |
number_of_minutes = StringVar() # needs to be string type, not integer type, because of Entry "textvariable" parameter requirements | |
# | |
# define the funtion that will be invoked when radio buttons are selected: | |
# | |
def display_total_charges(): | |
rate = cents_per_minute.get() | |
mins = number_of_minutes.get() | |
if mins == '': # if the string has not yet been input, it will default to '', which can't be converted to integer (throws: "ValueError: invalid literal for int() with base 10: ''"), so prompt the user to input | |
message = "Please enter the length of the call in minutes, then select a rate" | |
else: | |
mins = int(mins) | |
total_cents = rate * mins | |
total_dollars = total_cents / 100.00 | |
message = "Cents Per Minute: " + str(rate) + ". Number of Minutes: " + str(mins) + ". Total Charges (USD): $" + str(total_dollars) | |
label.config(text = message) | |
# | |
# define GUI elements: | |
# | |
minutes_input_label = Label(window, text="Length of Call (in minutes):") | |
minutes_input = Entry(window, textvariable=number_of_minutes) | |
daytime_button = Radiobutton(window, text="Daytime (6:00 am through 5:59 pm)", variable=cents_per_minute, value=7, command=display_total_charges) | |
evening_button = Radiobutton(window, text="Evening (6:00 pm through 11:59 pm)", variable=cents_per_minute, value=12, command=display_total_charges) | |
offpeak_button = Radiobutton(window, text="Off-Peak (midnight through 5:59 am)", variable=cents_per_minute, value=5,command=display_total_charges) | |
label = Label(window) | |
# | |
# package elements / bind to window: | |
# | |
minutes_input_label.pack() | |
minutes_input.pack() | |
daytime_button.pack(anchor = W) | |
evening_button.pack(anchor = W) | |
offpeak_button.pack(anchor = W) | |
label.pack() | |
# | |
# open main window / run application: | |
# | |
window.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment