説明なし

taschenrechner.py 2.6KB

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