Skip to content

Instantly share code, notes, and snippets.

@IanMcT
Last active September 26, 2020 22:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanMcT/4541cb9484cf241042592497b56c9666 to your computer and use it in GitHub Desktop.
Save IanMcT/4541cb9484cf241042592497b56c9666 to your computer and use it in GitHub Desktop.
Code for a windows program that takes input, runs code when button pressed output is created. Modify so anything over 300km has added output: Geez, that is far. Anything less than 50 km - Short trip!
#Ian McTavish
#July 26, 2016
#Create a window with an Entry widget
#import required libraries
import tkinter
import tkinter.messagebox #needed for messagebox
class MyGUI:
"""Graphics class"""
#constructor method
def __init__(self):
"""Initializes the window"""
#Create the main window widget
self.main_window = tkinter.Tk()
#Create two frames
self.top_frame = tkinter.Frame(self.main_window)
self.bottom_frame = tkinter.Frame(self.main_window)
#Top Frame widgets
self.label_prompt = tkinter.Label(self.top_frame,
text='Enter a distance in kilometers')
self.entry_kilo = tkinter.Entry(self.top_frame,
width=10)
#Pack top frame widgets
self.label_prompt.pack(side="left")
self.entry_kilo.pack(side="left")
#Create the bottom widgets
#Create a button Widget
#Text of button: Convert
#calls button_convert_click method
self.button_convert = tkinter.Button(self.bottom_frame,
text = "Convert",
command=self.button_convert_click)
#Create quit button - runs destroy method of the root widget
self.quit_button = tkinter.Button(self.bottom_frame,
text='Quit',
command=self.main_window.destroy)
#Pack the button
self.button_convert.pack(side="left")
self.quit_button.pack(side="left")
#Pack the frames
self.top_frame.pack()
self.bottom_frame.pack()
#enter the mainloop
tkinter.mainloop()
#convert button click is callback function for the Convert button
def button_convert_click(self):
#Get the value entered in the entry widget
kilo = float(self.entry_kilo.get())
#convert km to miles
miles = kilo*0.6214
#Display an info dialog box
tkinter.messagebox.showinfo("Results",
str(kilo) + " kilometers is "+
str(miles) + " miles.")
#Create an instance of the MyGUI class
print(MyGUI.__doc__)
my_gui = MyGUI()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment