from tkinter import *
import re
def c10to2( x) :
if x == '0' : return '0'
if re .match ( r'\d +\b ' , x) :
s = ''
x = int ( x)
while x >= 1 :
s+= str ( x%2 )
x//= 2
return s[ ::-1 ]
else :
return 'ERROR'
def c2to10( x) :
if re .match ( r'[01]+\b ' , x) :
x = str ( x)
y = 0
n = len ( x) -1
for i in x:
y+= int ( i) *( 2 **n)
n-= 1
return y
return 'ERROR'
def c10to16( x) :
d = { 10 :'A' , 11 :'B' , 12 :'C' ,
13 :'D' , 14 :'E' , 15 :'F' }
s = ''
x = int ( x)
while x >= 1 :
if x%16 > 9 :
s+= d.get ( x%16 )
else :
s+= str ( x%16 )
x//= 16
return s[ ::-1 ]
def c16to10( x) :
d = { 'A' :10 , 'B' :11 , 'C' :12 ,
'D' :13 , 'E' :14 , 'F' :15 }
x = x.upper ( )
if re .match ( r'[0-9A-F]+\b ' , x) :
n = len ( x) -1
y = 0
for elem in x:
if elem.isdigit ( ) :
y+= int ( elem) *( 16 **n)
else :
y+= d.get ( elem) *( 16 **n)
n-= 1
return y
else : return 'ERROR'
def c10to8( x) :
if re .match ( r'\d +\b ' , x) :
s = ''
x = int ( x)
while x >= 1 :
s+= str ( x%8 )
x//= 8
return s[ ::-1 ]
else : return 'ERROR'
def c8to10( x) :
x = str ( x)
if re .match ( r'[0-7]+\b ' , x) :
n = len ( x) -1
y = 0
for elem in x:
y+= int ( elem) *( 8 **n)
n-= 1
return y
else : return 'ERROR'
def etake( e) :
number = my_number.get ( )
x = qwe.get ( )
y = rty.get ( )
if x == 10 and y == 2 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c10to2( number) )
elif x == 2 and y == 10 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c2to10( number) )
elif x == 2 and y == 8 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c10to8( str ( c2to10( number) ) ) )
elif x == 2 and y == 16 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c10to16( str ( c2to10( number) ) ) )
elif x == 10 and y == 16 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c10to16( number) )
elif x == 8 and y == 2 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c10to2( str ( c8to10( number) ) ) )
elif x == 8 and y == 16 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c10to16( str ( c8to10( number) ) ) )
elif x == 16 and y == 2 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c10to2( str ( c16to10( number) ) ) )
elif x == 16 and y == 8 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c10to8( str ( c16to10( number) ) ) )
elif x == 16 and y == 10 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c16to10( number) )
elif x == 10 and y == 8 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c10to8( number) )
elif x == 8 and y == 10 :
my_solve.delete ( 0 , END)
my_solve.insert ( END, c8to10( number) )
elif x == y:
my_solve.delete ( 0 , END)
my_solve.insert ( END, number)
else :
return 'ERROR'
w = Tk( )
w.title ( 'Системы счисления' )
w.geometry ( '315x145' )
w.resizable ( 0 , 0 )
first_text = Label( w, text= 'Перевести из ' , font= 14 )
second_text = Label( w, text= 'в' , font= 14 )
third_text = Label( w, text= "Моё число: " , font= 14 )
qwe = IntVar( w)
rty = IntVar( w)
ch = [ 2 , 8 , 10 , 16 ]
my_sys = OptionMenu( w, qwe, *ch)
needed_sys = OptionMenu( w, rty, *ch)
my_number= Entry( w, width= 20 )
my_solve= Entry( w, width= 20 )
b = Button( w, text= 'Go!' , bg= 'pink' , height= 2 , width= 5 , command= lambda event= '<Button-1>' : etake( '<Button-1>' ) ) # Теперь результат выдаётся как по нажатию на кнопку "Go", так и...
my_number.bind ( '<Return>' , etake) # ... по простому нажатию на Enter
first_text.place ( x= 6 , y= 10 )
my_sys.place ( x= 130 , y= 10 )
second_text.place ( x= 195 , y= 10 )
third_text.place ( x= 6 , y= 42 )
needed_sys.place ( x= 222 , y= 10 )
my_number.place ( x= 6 , y= 70 )
b.place ( x= 135 , y= 65 )
my_solve.place ( x= 185 , y= 70 )
w.mainloop ( )
ZnJvbSB0a2ludGVyIGltcG9ydCAqCmltcG9ydCByZQpkZWYgYzEwdG8yKHgpOgogICAgaWYgeCA9PSAnMCc6IHJldHVybiAnMCcKICAgIGlmIHJlLm1hdGNoKHInXGQrXGInLHgpOgogICAgICAgIHMgPSAnJwogICAgICAgIHggPSBpbnQoeCkKICAgICAgICB3aGlsZSB4ID49MToKICAgICAgICAgICAgcys9c3RyKHglMikKICAgICAgICAgICAgeC8vPTIKICAgICAgICByZXR1cm4gc1s6Oi0xXQogICAgZWxzZToKICAgICAgICByZXR1cm4gJ0VSUk9SJwpkZWYgYzJ0bzEwKHgpOgogICAgaWYgcmUubWF0Y2gocidbMDFdK1xiJyx4KToKICAgICAgICB4ID0gc3RyKHgpCiAgICAgICAgeSA9IDAKICAgICAgICBuID0gbGVuKHgpLTEKICAgICAgICBmb3IgaSBpbiB4OgogICAgICAgICAgICB5Kz1pbnQoaSkqKDIqKm4pCiAgICAgICAgICAgIG4tPTEKICAgICAgICByZXR1cm4geQogICAgcmV0dXJuICdFUlJPUicKZGVmIGMxMHRvMTYoeCk6CiAgICBkID0gezEwOidBJywxMTonQicsMTI6J0MnLAogICAgICAgICAxMzonRCcsMTQ6J0UnLDE1OidGJ30KICAgIHMgPSAnJwogICAgeCA9IGludCh4KQogICAgd2hpbGUgeCA+PSAxOgogICAgICAgIGlmIHglMTYgPiA5OgogICAgICAgICAgICBzKz0gZC5nZXQoeCUxNikKICAgICAgICBlbHNlOgogICAgICAgICAgICBzKz1zdHIoeCUxNikKICAgICAgICB4Ly89MTYKICAgIHJldHVybiBzWzo6LTFdCmRlZiBjMTZ0bzEwKHgpOgogICAgZCA9IHsnQSc6MTAsJ0InOjExLCdDJzoxMiwKICAgICAgICAgJ0QnOjEzLCdFJzoxNCwnRic6MTV9CiAgICB4ID0geC51cHBlcigpCiAgICBpZiByZS5tYXRjaChyJ1swLTlBLUZdK1xiJyx4KToKICAgICAgICBuID0gbGVuKHgpLTEKICAgICAgICB5ID0gMAogICAgICAgIGZvciBlbGVtIGluIHg6CiAgICAgICAgICAgIGlmIGVsZW0uaXNkaWdpdCgpOgogICAgICAgICAgICAgICAgeSs9aW50KGVsZW0pKigxNioqbikKICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgIHkrPSBkLmdldChlbGVtKSooMTYqKm4pCiAgICAgICAgICAgIG4tPTEKICAgICAgICByZXR1cm4geQogICAgZWxzZTogcmV0dXJuICdFUlJPUicKZGVmIGMxMHRvOCh4KToKICAgIGlmIHJlLm1hdGNoKHInXGQrXGInLHgpOgogICAgICAgIHMgPSAnJwogICAgICAgIHggPSBpbnQoeCkKICAgICAgICB3aGlsZSB4ID49MToKICAgICAgICAgICAgcys9c3RyKHglOCkKICAgICAgICAgICAgeC8vPTgKICAgICAgICByZXR1cm4gc1s6Oi0xXQogICAgZWxzZTogcmV0dXJuICdFUlJPUicKZGVmIGM4dG8xMCh4KToKICAgIHggPSBzdHIoeCkKICAgIGlmIHJlLm1hdGNoKHInWzAtN10rXGInLHgpOgogICAgICAgIG4gPSBsZW4oeCktMQogICAgICAgIHkgPSAwCiAgICAgICAgZm9yIGVsZW0gaW4geDoKICAgICAgICAgICAgeSs9aW50KGVsZW0pKig4KipuKQogICAgICAgICAgICBuLT0xCiAgICAgICAgcmV0dXJuIHkKICAgIGVsc2U6IHJldHVybiAnRVJST1InCgpkZWYgZXRha2UoZSk6CiAgICBudW1iZXIgPSBteV9udW1iZXIuZ2V0KCkKICAgIHggPSBxd2UuZ2V0KCkKICAgIHkgPSBydHkuZ2V0KCkKICAgIGlmIHggPT0gMTAgYW5kIHkgPT0gMjoKICAgICAgICBteV9zb2x2ZS5kZWxldGUoMCxFTkQpCiAgICAgICAgbXlfc29sdmUuaW5zZXJ0KEVORCxjMTB0bzIobnVtYmVyKSkKICAgIGVsaWYgeCA9PSAyIGFuZCB5ID09IDEwOgogICAgICAgIG15X3NvbHZlLmRlbGV0ZSgwLEVORCk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