from Tkinter import * from tkMessageBox import showinfo #---------------------------------- def response(event): I = event.widget['text'] texts = UPN.text.get(1.0,END+'-1c') #print texts if I == 'DEL': UPN.text.delete(INSERT + '-1c',INSERT) elif I == 'Clear': UPN.text.delete(1.0,END) elif I == 'Enter': UPN.text.insert(INSERT,'\n') elif I == '=': try: s = toList(texts) #print s UPN.text.insert(END,'\n=\n') UPN.text.insert(END,f(s)) UPN.text.see('end') except: showinfo('Achtung:', 'Falsche Eingabe,bitte ueberpruefen Sie die Eingabe!') else: UPN.text.insert(INSERT,I) def UPN(): root.title('umgekehrte polnische Notation') root.geometry('310x440+200+100') UI = LabelFrame(root) UI.place(x = 1,y = 1,width = 308,height = 124) UPN.text = Text(UI) UPN.text.place(x=1,y=1,width=300,height=116) Li=[['7','8','9','+','DEL'], ['4','5','6','-','Enter'], ['1','2','3','*','='], ['0','00','.','/','Clear']] for i in range(len(Li[0])): for j in range(len(Li)): bt = Button(root) bt.place(x=i*60+10,y=120+(j+1)*60,width=50,height=50) bt['text'] = Li[j][i] bt.bind('',response,add=(root,)) #---------------------------------- def toList(text): a = text.splitlines() L = [] for i in a: s = "" z = True for j in i: #print j if j not in '+-*/': s += j z = True else: if s: L.append(s) if j: L.append(j) s = "" z = False if i[-1] in '+-*/': if z and j: L.append(j) else: if z and s: L.append(s) return L def Stack(i,s,S = None): if not S: S = [] if i == len(s): return S[0] a = s[i] if a.isdigit(): S.append(a) return Stack(s, S, i+1) else: c = S.pop() d = S.pop() S.append(Grundrechenarten(d,a,c)) return Stack(s, S, i+1) def Grundrechenarten(c,a,d): if a == '+': result = float(c)+float(d) elif a == '-': result = float(c)-float(d) elif a == '*': result = float(c)*float(d) elif a == '/': result = float(c)/float(d) if result == int(result): result = int(result) return result #---------------------------------- def main(): UPN() root.mainloop() if __name__ == '__main__': print 'Dieser Taschenrechner ist mit UPN-Eingabe moeglich.' root = Tk() main()