Nenhuma Descrição

taschenrechner.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from Tkinter import *
  2. from tkMessageBox import showinfo
  3. #----------------------------------
  4. def response(event):
  5. I = event.widget['text']
  6. texts = UPN.text.get(1.0,END+'-1c')
  7. #print texts
  8. if I == 'DEL':
  9. UPN.text.delete(INSERT + '-1c',INSERT)
  10. elif I == 'Clear':
  11. UPN.text.delete(1.0,END)
  12. elif I == 'Enter':
  13. UPN.text.insert(INSERT,'\n')
  14. elif I == '=':
  15. try:
  16. s = toList(texts)
  17. #print s
  18. UPN.text.insert(END,'\n=\n')
  19. UPN.text.insert(END,f(s))
  20. UPN.text.see('end')
  21. except:
  22. showinfo('Achtung:', 'Falsche Eingabe,bitte ueberpruefen Sie die Eingabe!')
  23. else:
  24. UPN.text.insert(INSERT,I)
  25. def UPN():
  26. root.title('umgekehrte polnische Notation')
  27. root.geometry('310x440+200+100')
  28. UI = LabelFrame(root)
  29. UI.place(x = 1,y = 1,width = 308,height = 124)
  30. UPN.text = Text(UI)
  31. UPN.text.place(x=1,y=1,width=300,height=116)
  32. Li=[['7','8','9','+','DEL'],
  33. ['4','5','6','-','Enter'],
  34. ['1','2','3','*','='],
  35. ['0','00','.','/','Clear']]
  36. for i in range(len(Li[0])):
  37. for j in range(len(Li)):
  38. bt = Button(root)
  39. bt.place(x=i*60+10,y=120+(j+1)*60,width=50,height=50)
  40. bt['text'] = Li[j][i]
  41. bt.bind('<Button-1>',response,add=(root,))
  42. #----------------------------------
  43. def toList(text):
  44. a = text.splitlines()
  45. L = []
  46. for i in a:
  47. s = ""
  48. z = True
  49. for j in i:
  50. #print j
  51. if j not in '+-*/':
  52. s += j
  53. z = True
  54. else:
  55. if s:
  56. L.append(s)
  57. if j:
  58. L.append(j)
  59. s = ""
  60. z = False
  61. if i[-1] in '+-*/':
  62. if z and j:
  63. L.append(j)
  64. else:
  65. if z and s:
  66. L.append(s)
  67. return L
  68. def Stack(i,s,S = None):
  69. if not S:
  70. S = []
  71. if i == len(s):
  72. return S[0]
  73. a = s[i]
  74. if a.isdigit():
  75. S.append(a)
  76. return Stack(s, S, i+1)
  77. else:
  78. c = S.pop()
  79. d = S.pop()
  80. S.append(Grundrechenarten(d,a,c))
  81. return Stack(s, S, i+1)
  82. def Grundrechenarten(c,a,d):
  83. if a == '+':
  84. result = float(c)+float(d)
  85. elif a == '-':
  86. result = float(c)-float(d)
  87. elif a == '*':
  88. result = float(c)*float(d)
  89. elif a == '/':
  90. result = float(c)/float(d)
  91. if result == int(result):
  92. result = int(result)
  93. return result
  94. #----------------------------------
  95. def main():
  96. UPN()
  97. root.mainloop()
  98. if __name__ == '__main__':
  99. print 'Dieser Taschenrechner ist mit UPN-Eingabe moeglich.'
  100. root = Tk()
  101. main()