fork download
  1. from tkinter import *
  2. import re
  3. def c10to2(x):
  4. if x == '0': return '0'
  5. if re.match(r'\d+\b',x):
  6. s = ''
  7. x = int(x)
  8. while x >=1:
  9. s+=str(x%2)
  10. x//=2
  11. return s[::-1]
  12. else:
  13. return 'ERROR'
  14. def c2to10(x):
  15. if re.match(r'[01]+\b',x):
  16. x = str(x)
  17. y = 0
  18. n = len(x)-1
  19. for i in x:
  20. y+=int(i)*(2**n)
  21. n-=1
  22. return y
  23. return 'ERROR'
  24. def c10to16(x):
  25. d = {10:'A',11:'B',12:'C',
  26. 13:'D',14:'E',15:'F'}
  27. s = ''
  28. x = int(x)
  29. while x >= 1:
  30. if x%16 > 9:
  31. s+= d.get(x%16)
  32. else:
  33. s+=str(x%16)
  34. x//=16
  35. return s[::-1]
  36. def c16to10(x):
  37. d = {'A':10,'B':11,'C':12,
  38. 'D':13,'E':14,'F':15}
  39. x = x.upper()
  40. if re.match(r'[0-9A-F]+\b',x):
  41. n = len(x)-1
  42. y = 0
  43. for elem in x:
  44. if elem.isdigit():
  45. y+=int(elem)*(16**n)
  46. else:
  47. y+= d.get(elem)*(16**n)
  48. n-=1
  49. return y
  50. else: return 'ERROR'
  51. def c10to8(x):
  52. if re.match(r'\d+\b',x):
  53. s = ''
  54. x = int(x)
  55. while x >=1:
  56. s+=str(x%8)
  57. x//=8
  58. return s[::-1]
  59. else: return 'ERROR'
  60. def c8to10(x):
  61. x = str(x)
  62. if re.match(r'[0-7]+\b',x):
  63. n = len(x)-1
  64. y = 0
  65. for elem in x:
  66. y+=int(elem)*(8**n)
  67. n-=1
  68. return y
  69. else: return 'ERROR'
  70.  
  71. def etake(e):
  72. number = my_number.get()
  73. x = qwe.get()
  74. y = rty.get()
  75. if x == 10 and y == 2:
  76. my_solve.delete(0,END)
  77. my_solve.insert(END,c10to2(number))
  78. elif x == 2 and y == 10:
  79. my_solve.delete(0,END)
  80. my_solve.insert(END,c2to10(number))
  81. elif x == 2 and y == 8:
  82. my_solve.delete(0,END)
  83. my_solve.insert(END,c10to8(str(c2to10(number))))
  84. elif x == 2 and y == 16:
  85. my_solve.delete(0,END)
  86. my_solve.insert(END,c10to16(str(c2to10(number))))
  87. elif x == 10 and y == 16:
  88. my_solve.delete(0,END)
  89. my_solve.insert(END, c10to16(number))
  90. elif x == 8 and y == 2:
  91. my_solve.delete(0,END)
  92. my_solve.insert(END, c10to2(str(c8to10(number))))
  93. elif x == 8 and y == 16:
  94. my_solve.delete(0,END)
  95. my_solve.insert(END, c10to16(str(c8to10(number))))
  96. elif x == 16 and y == 2:
  97. my_solve.delete(0,END)
  98. my_solve.insert(END, c10to2(str(c16to10(number))))
  99. elif x == 16 and y == 8:
  100. my_solve.delete(0,END)
  101. my_solve.insert(END, c10to8(str(c16to10(number))))
  102. elif x == 16 and y == 10:
  103. my_solve.delete(0,END)
  104. my_solve.insert(END, c16to10(number))
  105. elif x == 10 and y == 8:
  106. my_solve.delete(0,END)
  107. my_solve.insert(END, c10to8(number))
  108. elif x == 8 and y == 10:
  109. my_solve.delete(0,END)
  110. my_solve.insert(END, c8to10(number))
  111. elif x == y:
  112. my_solve.delete(0,END)
  113. my_solve.insert(END, number)
  114. else:
  115. return 'ERROR'
  116. w = Tk()
  117.  
  118. w.title('Системы счисления')
  119. w.geometry('315x145')
  120. w.resizable(0,0)
  121. first_text = Label(w,text='Перевести из ',font=14)
  122. second_text = Label(w,text='в',font=14)
  123. third_text = Label(w,text="Моё число: ",font=14)
  124. qwe = IntVar(w)
  125. rty = IntVar(w)
  126. ch = [2,8,10,16]
  127. my_sys = OptionMenu(w,qwe,*ch)
  128. needed_sys = OptionMenu(w,rty,*ch)
  129.  
  130. my_number=Entry(w,width=20)
  131. my_solve=Entry(w,width=20)
  132. b = Button(w,text='Go!',bg='pink',height=2,width=5,command=lambda event='<Button-1>': etake('<Button-1>')) # Теперь результат выдаётся как по нажатию на кнопку "Go", так и...
  133. my_number.bind('<Return>',etake) # ... по простому нажатию на Enter
  134.  
  135. first_text.place(x=6,y=10)
  136. my_sys.place(x=130,y=10)
  137. second_text.place(x=195,y=10)
  138. third_text.place(x=6,y=42)
  139. needed_sys.place(x=222,y=10)
  140. my_number.place(x=6, y=70)
  141. b.place(x=135,y=65)
  142. my_solve.place(x=185,y=70)
  143.  
  144. w.mainloop()
  145.  
Runtime error #stdin #stdout #stderr 0.05s 26088KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 116, in <module>
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1854, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable