this is a simple calculator maked whith python and gtk.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | import pygtk,math,string
pygtk.require('2.0')
import gtk
class mainwin():
def __init__(self): #This function autorun at assign object to class >> "win=mainwin()"
self.mwin=gtk.Window()
self.mwin.set_size_request(300,270)
self.mwin.set_resizable(False)
self.mwin.set_title('Calculator')
#=============== Create menu bar and popups ===============
self.menus=(
("/_Calculator",None,None,0,"<Branch>"),
("/Calculator/_CE","<Control>C",self.ce,0,"<StockItem>",gtk.STOCK_CANCEL),
("/Calculator/C_lear","<Control>L",self.clear,0,"<StockItem>",gtk.STOCK_CLEAR),
("/Calculator/_Bksp","<Backspace>",self.bksp,0,"<StockItem>",gtk.STOCK_GO_BACK ),
("/Calculator/sep1",None,None,0,"<Separator>"),
("/Calculator/_Quit","<Control>Q",gtk.main_quit,0,"<StockItem>",gtk.STOCK_QUIT),
)
#======== Create map of buttons and clicked event =========
ButtonsProp=(
(("Bksp",self.bksp) ,("Clear",self.clear) ,("CE",self.ce)),
(("7",self.calc_numbers) ,("8",self.calc_numbers) ,(" 9 ",self.calc_numbers) ,(" / " ,self.calc_operators),("sqrt",self.sqrt)),
(("4" ,self.calc_numbers) ,("5",self.calc_numbers) ,(" 6 ",self.calc_numbers) ,(" * ",self.calc_operators) ,("%" ,self.percent)),
(("1",self.calc_numbers) ,("2" ,self.calc_numbers),(" 3 " ,self.calc_numbers),(" - ",self.calc_operators) ,("1/x",self.one_div_x)),
(("0",self.calc_numbers) ,("+/-",self.change_sign),(" . ",self.dot) ,(" + " ,self.calc_operators),("=",self.do_equal))
)
#================ Create entry and buttons ================
self.accelg=gtk.AccelGroup()
itemfac=gtk.ItemFactory(gtk.MenuBar,"<main>")
itemfac.create_items(self.menus)
self.mwin.add_accel_group(self.accelg)
self.itemfac=itemfac
self.menubar=itemfac.get_widget("<main>")
self.vb1=gtk.VBox(0,0)
self.vb1.pack_start(self.menubar,0,0,0)
self.mwin.add(self.vb1)
self.mtext=gtk.Entry()
self.mtext.set_text("0")
self.mtext.set_editable(False)
self.hb1=gtk.HBox()
self.hb1.pack_start(self.mtext,1,1,4)
self.vb1.pack_start(self.hb1,0,0,3)
self.mtable=gtk.Table(4,3)
self.mtable.set_row_spacings(3)
self.mtable.set_col_spacings(3)
x=y=0
for i in ButtonsProp:
for j in i:
btn=gtk.Button(j[0])
btn.connect("clicked",j[1])
self.mtable.attach(btn,x,x+1,y,y+1)
x+=1
print j[0] ," ",
x=0
y+=1
print ""
self.hb2=gtk.HBox()
self.hb2.pack_start(self.mtable,1,1,4)
self.vb1.pack_start(self.hb2,1,1,2)
#============= set flags =============
self.zero=True
self.equal=False
self.oldnum=0
self.operator=""
# { The End }
def do_equal(self,widget):
if self.oldnum != 0:
if not self.equal: self.currentnum=string.atof(self.mtext.get_text())
if self.operator==" / ":
if self.currentnum==0: return 1
self.mtext.set_text(str(self.oldnum/self.currentnum));self.oldnum=string.atof(self.mtext.get_text())
elif self.operator==" * ":
self.mtext.set_text(str(self.oldnum*self.currentnum));self.oldnum=string.atof(self.mtext.get_text())
elif self.operator==" - ":
self.mtext.set_text(str(self.oldnum-self.currentnum));self.oldnum=string.atof(self.mtext.get_text())
elif self.operator==" + ":
self.mtext.set_text(str(self.oldnum+self.currentnum));self.oldnum=string.atof(self.mtext.get_text())
self.clear_dot_zero()
self.equal=True
def calc_numbers(self,widget):
if self.zero==True :
self.mtext.set_text("")
self.zero=False
if self.mtext.get_text()=="0": self.mtext.set_text("")
self.mtext.set_text(self.mtext.get_text()+ str(string.atoi( widget.get_label())))
def calc_operators(self,widget):
self.oldnum=string.atof(self.mtext.get_text())
self.operator=widget.get_label()
self.zero=True
self.equal=False
def change_sign(self,widget):
self.mtext.set_text(str(string.atof(self.mtext.get_text())* -1))
self.clear_dot_zero()
def dot(self,widget):
if self.zero==True :
self.oldnum=string.atof(self.mtext.get_text())
self.mtext.set_text("0")
self.zero=False
if string.find(self.mtext.get_text(),".")<=0:
self.mtext.set_text(self.mtext.get_text()+".")
self.zero=False
def sqrt(self,widget):
self.mtext.set_text(str(math.sqrt(string.atof(self.mtext.get_text()))))
self.clear_dot_zero()
self.zero=True
def percent(self,widget):
if (self.oldnum != 0)and(string.atof(self.mtext.get_text()) != 0):
self.mtext.set_text(str(string.atof(self.mtext.get_text())*(self.oldnum*0.01)))
self.clear_dot_zero()
def one_div_x(self,widget):
if string.atof(self.mtext.get_text()) != 0:
self.mtext.set_text(str(1/string.atof(self.mtext.get_text())))
self.clear_dot_zero()
self.zero=True
def bksp(self,widget,e=0):
self.mtext.set_text(self.mtext.get_text()[0:-1])
if self.mtext.get_text()=="":
self.mtext.set_text("0")
self.zero=True
def clear(self,widget,e=0):
self.mtext.set_text("0")
self.zero=True
def ce(self,widget,e=0):
self.mtext.set_text("0")
self.operator=""
self.oldnum=0
self.zero=True
def clear_dot_zero(self):
if self.mtext.get_text()[-2:]==".0":
self.mtext.set_text(self.mtext.get_text()[0:-2])
def main(self):
self.mwin.connect("destroy",gtk.main_quit)
self.mwin.show_all()
gtk.main()
if __name__=='__main__':
win=mainwin()
win.main()
|
Tags: calculator, pygtk
string.atoi()
is deprecated. you should useint()
instead