import Tkinter from Tkinter import * #creates an object of type frame from Tkinter class Application(Frame): #starting this application with these available to use def __init__(self, master): Frame.__init__(self, master) self.grid() #extremely useful, used for placement of widgets self.create_widget() #same as above but for creation instead of placement #this is where all of our widget objects go, and most of our code. #seems tedious but does the job. def create_widget(self): #an Entry object lets us type in it, and then pull info from it #sticky just makes the widget object fit inside the placement box #based on the directions specified N-orth S-outh E-ast W-est self.num_1 = Entry(self) self.num_1.grid(row = 1, column = 3, sticky = W) self.num_2 = Entry(self) self.num_2.grid(row = 2, column = 3, sticky = W) #creates a Label widget based on a global variable I define later if this_fun == function[0]: self.inst_lbl = Label(self, text = "+___________________") self.inst_lbl.grid(row = 3, column = 3, sticky = W) elif this_fun == function[1]: self.inst_lbl = Label(self, text = "-____________________") self.inst_lbl.grid(row = 3, column = 3, sticky = W) elif this_fun == function[2]: self.inst_lbl = Label(self, text = "x___________________") self.inst_lbl.grid(row = 3, column = 3, sticky = W) else: self.inst_lbl = Label(self, text = "/___________________") self.inst_lbl.grid(row = 3, column = 3, sticky = W) #create submit button, you will notice something new about this, the command #variable. very handy and we shall use more later self.submit_bttn = Button(self, text = " = ", command = self.reveal) self.submit_bttn.grid(row = 3, column = 0,columnspan = 2, sticky = W+E) #used to change the Label string and value of global "this_fun" self.submit_bttn = Button(self, text = " + ", command = self.add) self.submit_bttn.grid(row = 1, column = 0, sticky = W+E) self.submit_bttn = Button(self, text = " - ", command = self.subtract) self.submit_bttn.grid(row = 1, column = 1, sticky = W+E) self.submit_bttn = Button(self, text = " x ", command = self.multiply) self.submit_bttn.grid(row = 2, column = 0, sticky = W+E) self.submit_bttn = Button(self, text = " / ", command = self.divide) self.submit_bttn.grid(row = 2, column = 1, sticky = W+E) #Time for a new type of object, the Check Buttons #for these we need a boolean variable we can change self.use_again = BooleanVar() Checkbutton(self, text = "Use equated value for next calculation", command = self.place_it ).grid(row = 5, column = 0, columnspan = 3, sticky = W+E) #create a display Widget width for num of characters per line #height for lines of characters, wrap does well..... wrap kinda like #notepad would. very handy self.secret_txt = Text(self, width = 20, height = 1, wrap = WORD) self.secret_txt.grid(row = 4, column = 3,sticky = W) def place_it(self): global use_num use_num = self.calculate_int() def calculate_int(self): if this_fun == function[0]: done_val = int(self.num_1.get()) + int(self.num_2.get()) return int(self.num_1.get()) + int(self.num_2.get()) elif this_fun == function[1]: done_val = int(self.num_1.get()) - int(self.num_2.get()) return int(self.num_1.get()) - int(self.num_2.get()) elif this_fun == function[2]: done_val = int(self.num_1.get()) * int(self.num_2.get()) return int(self.num_1.get()) * int(self.num_2.get()) else: done_val = int(self.num_1.get()) / int(self.num_2.get()) return int(self.num_1.get()) / int(self.num_2.get()) def reveal(self): msg = str(self.calculate_int()) + " this_fun =" + this_fun + " " + str(use_num) self.secret_txt.delete(0.0, END) self.secret_txt.insert(0.0, msg ) def add(self): global this_fun self.inst_lbl = Label(self, text = "+___________________") self.inst_lbl.grid(row = 3, column = 3, sticky = W) this_fun = function[0] def subtract(self): global this_fun self.inst_lbl = Label(self, text = "-____________________") self.inst_lbl.grid(row = 3, column = 3, sticky = W) this_fun = function[1] def multiply(self): global this_fun self.inst_lbl = Label(self, text = "x___________________") self.inst_lbl.grid(row = 3, column = 3, sticky = W) this_fun = function[2] def divide(self): global this_fun self.inst_lbl = Label(self, text = "/___________________") self.inst_lbl.grid(row = 3, column = 3, sticky = W) this_fun = function[3] function = ["+","-","x","/"] this_fun = function[0] use_num = 0 done_val = 0 root = Tk() root.title("Simple Calculator") root.geometry("400x200") app = Application(root) root.mainloop()