Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter #<imports kinter module
- from tkinter import * #<and * from the tkinter module
- from tkinter import Tk #<and Tk from the tkinter module
- import sys#imports sys (allows to use functions such as timesleep
- convert_formula = 25.4#the number needed to divide by to convert MM to Inches as a variable
- root = Tk()#sets root = Tk()
- root.title('Converter')#creates a title in the Tk Widget
- Label(text = 'Welcome to the converter').pack(side = TOP, padx = 10, pady = 10)#Just a little intro in the Tk widget
- def convert(Num_In_MM):#This is the conversion function, sets the rules and restrictions when converting a unit of measure from MM to Inches
- Num_In_MM = float(e.get())#Gets user input from the entry widget created by tkinter (see below)
- Results = ("%.3f" % (Num_In_MM / convert_formula))#creates a variable from the users input divided by convert_fomula(25.4) and saves the results to the 3rd decimal places (thousanths)
- print(Results)#Prints the float stored in the variable Results
- def quit():#Creates a quit function for the quit button on the Tk Widget
- global root
- root.destroy()#destroys the widget(closes it)
- e = Entry(root, width = 5)#Creates an entry box to get user input
- e.bind('<Return>', convert)#binds the entry box to <Return> (Enter key) so the user can hit enter to get the results
- e.pack(side = TOP, padx = 10, pady = 10)#this just packs the widget
- Button(root, text='quit', command=quit).pack(side= RIGHT)#creates a quit button on the widget
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment