1
+ from tobinary import decimalToBinary , octalToBinary , hexToBinary
2
+ from tooctal import decimalToOctal , binaryToOctal , hexToOctal
3
+ from tohex import decimalToHex , binaryToHex , octalToHex
4
+ from todecimal import binaryToDecimal , octalToDecimal , hexToDecimal
5
+
6
+ from tkinter import *
7
+ from tkinter import messagebox
8
+
9
+ root = Tk ()
10
+ root .title ("Number Base Conversion" )
11
+ root .state ('zoomed' )
12
+ root .configure (background = "#011" )
13
+
14
+ # variables-----------------------------------------------------------------------------------------------------------
15
+ global decimal_ip , binary_ip , octal_ip , hexadecimal_ip
16
+ decimal_ip = StringVar ()
17
+ binary_ip = StringVar ()
18
+ octal_ip = StringVar ()
19
+ hexadecimal_ip = StringVar ()
20
+
21
+ # functions-----------------------------------------------------------------------------------------------------------
22
+ def convert ():
23
+ decimal = decimal_ip .get ()
24
+ binary = binary_ip .get ()
25
+ octal = octal_ip .get ()
26
+ hex = hexadecimal_ip .get ()
27
+
28
+ result = False
29
+
30
+ if decimal :
31
+ try :
32
+ float (decimal )
33
+ result = True
34
+ except :
35
+ messagebox .showerror ("Error" , "Invalid Decimal Value" , parent = root )
36
+ decimal_ip .set ('' )
37
+
38
+ if result :
39
+ b = decimalToBinary (decimal )
40
+ o = decimalToOctal (decimal )
41
+ h = decimalToHex (decimal )
42
+
43
+ binary_ip .set (b )
44
+ octal_ip .set (o )
45
+ hexadecimal_ip .set (h )
46
+
47
+ if binary :
48
+ s = set (binary )
49
+ if '.' in s or '0' in s or '1' in s :
50
+ result = True
51
+ else :
52
+ messagebox .showerror ("Error" , "Invalid Binary Value" , parent = root )
53
+ binary_ip .set ('' )
54
+
55
+ if result :
56
+ try :
57
+ d = binaryToDecimal (binary )
58
+ o = binaryToOctal (binary )
59
+ h = binaryToHex (binary )
60
+
61
+ decimal_ip .set (d )
62
+ octal_ip .set (o )
63
+ hexadecimal_ip .set (h )
64
+
65
+ except :
66
+ messagebox .showerror ("Error" , "Invalid Binary Value" , parent = root )
67
+ binary_ip .set ('' )
68
+
69
+ if octal :
70
+ try :
71
+ o = float (octal )
72
+ if '8' in str (o ) or '9' in str (o ):
73
+ messagebox .showerror ("Error" , "Invalid Octal Value" , parent = root )
74
+ octal_ip .set ('' )
75
+ else :
76
+ result = True
77
+ except :
78
+ messagebox .showerror ("Error" , "Invalid Octal Value" , parent = root )
79
+ octal_ip .set ('' )
80
+
81
+ if result :
82
+ try :
83
+ d = octalToDecimal (octal )
84
+ b = octalToBinary (octal )
85
+ h = octalToHex (octal )
86
+
87
+ decimal_ip .set (d )
88
+ binary_ip .set (b )
89
+ hexadecimal_ip .set (h )
90
+
91
+ except :
92
+ messagebox .showerror ("Error" , "Invalid Octal Value" , parent = root )
93
+ octal_ip .set ('' )
94
+
95
+ if hex :
96
+ result = True
97
+ for h in hex .upper ():
98
+ if h == '.' :
99
+ pass
100
+ elif ((h < '0' or h > '9' ) and (h < 'A' or h > 'F' )):
101
+ result = False
102
+ break
103
+
104
+ if result :
105
+ try :
106
+ d = hexToDecimal (hex )
107
+ b = hexToBinary (hex )
108
+ o = hexToOctal (hex )
109
+
110
+ decimal_ip .set (d )
111
+ binary_ip .set (b )
112
+ octal_ip .set (o )
113
+
114
+ except :
115
+ messagebox .showerror ("Error" , "Invalid Hexadecimal Value" , parent = root )
116
+ hexadecimal_ip .set ('' )
117
+
118
+ else :
119
+ messagebox .showerror ("Error" , "Invalid Hexadecimal Value" , parent = root )
120
+ hexadecimal_ip .set ('' )
121
+
122
+ def clear ():
123
+ decimal_ip .set ('' )
124
+ binary_ip .set ('' )
125
+ octal_ip .set ('' )
126
+ hexadecimal_ip .set ('' )
127
+
128
+ # widgets-------------------------------------------------------------------------------------------------------------
129
+ title = Label (
130
+ root ,
131
+ text = "Number Base Conversion" ,
132
+ fg = "#0c0" ,
133
+ bg = "#011" ,
134
+ font = ("verdana" , 30 , "bold" ),
135
+ pady = 20 ,
136
+ ).pack (fill = X )
137
+
138
+ F1 = LabelFrame (
139
+ root ,
140
+ bd = 0 ,
141
+ font = ("verdana" , 15 , "bold" ),
142
+ bg = "#011" ,
143
+ )
144
+ F1 .place (relx = 0.5 , rely = 0.5 , anchor = CENTER )
145
+
146
+ decimal_lbl = Label (
147
+ F1 ,
148
+ text = "Decimal No. :" ,
149
+ font = ("verdana" , 20 , "bold" ),
150
+ bg = "#011" ,
151
+ fg = "#fff" ,
152
+ ).grid (sticky = E , row = 0 , column = 0 , padx = 20 , pady = 20 , ipadx = 10 , ipady = 10 )
153
+
154
+ decimal_txt = Entry (
155
+ F1 ,
156
+ width = 25 ,
157
+ textvariable = decimal_ip ,
158
+ font = "arial 15" ,
159
+ bd = 7 ,
160
+ relief = SUNKEN
161
+ ).grid (row = 0 , column = 1 , padx = 20 , pady = 20 , ipadx = 10 , ipady = 10 )
162
+
163
+ binary_lbl = Label (
164
+ F1 ,
165
+ text = "Binary No. :" ,
166
+ font = ("verdana" , 20 , "bold" ),
167
+ bg = "#011" ,
168
+ fg = "#fff" ,
169
+ ).grid (sticky = E , row = 1 , column = 0 , padx = 20 , pady = 20 , ipadx = 10 , ipady = 10 )
170
+
171
+ binary_txt = Entry (
172
+ F1 ,
173
+ width = 25 ,
174
+ textvariable = binary_ip ,
175
+ font = "arial 15" ,
176
+ bd = 7 ,
177
+ relief = SUNKEN
178
+ ).grid (row = 1 , column = 1 , padx = 20 , pady = 20 , ipadx = 10 , ipady = 10 )
179
+
180
+ octal_lbl = Label (
181
+ F1 ,
182
+ text = "Octal No. :" ,
183
+ font = ("verdana" , 20 , "bold" ),
184
+ bg = "#011" ,
185
+ fg = "#fff" ,
186
+ ).grid (sticky = E , row = 2 , column = 0 , padx = 20 , pady = 20 , ipadx = 10 , ipady = 10 )
187
+
188
+ octal_txt = Entry (
189
+ F1 ,
190
+ width = 25 ,
191
+ textvariable = octal_ip ,
192
+ font = "arial 15" ,
193
+ bd = 7 ,
194
+ relief = SUNKEN
195
+ ).grid (row = 2 , column = 1 , padx = 20 , pady = 20 , ipadx = 10 , ipady = 10 )
196
+
197
+ hexadecimal_lbl = Label (
198
+ F1 ,
199
+ text = "Hexadecimal No. :" ,
200
+ font = ("verdana" , 20 , "bold" ),
201
+ bg = "#011" ,
202
+ fg = "#fff" ,
203
+ ).grid (sticky = E , row = 3 , column = 0 , padx = 20 , pady = 20 , ipadx = 10 , ipady = 10 )
204
+
205
+ hexadecimal_txt = Entry (
206
+ F1 ,
207
+ width = 25 ,
208
+ textvariable = hexadecimal_ip ,
209
+ font = "arial 15" ,
210
+ bd = 7 ,
211
+ relief = SUNKEN
212
+ ).grid (row = 3 , column = 1 , padx = 20 , pady = 20 , ipadx = 10 , ipady = 10 )
213
+
214
+ convert_btn = Button (
215
+ F1 ,
216
+ text = "Convert" ,
217
+ command = convert ,
218
+ width = 10 ,
219
+ bd = 7 ,
220
+ font = "verdana 20 bold" ,
221
+ ).grid (row = 1 , column = 4 , columnspan = 2 , padx = 20 , pady = 20 , ipadx = 5 , ipady = 5 )
222
+
223
+ clear_btn = Button (
224
+ F1 ,
225
+ text = "Clear" ,
226
+ command = clear ,
227
+ width = 10 ,
228
+ bd = 7 ,
229
+ font = "verdana 20 bold" ,
230
+ ).grid (row = 2 , column = 4 , columnspan = 2 , padx = 20 , pady = 20 , ipadx = 5 , ipady = 5 )
231
+
232
+ quit_btn = Button (
233
+ F1 ,
234
+ text = "Quit" ,
235
+ command = root .quit ,
236
+ width = 10 ,
237
+ bd = 7 ,
238
+ font = "verdana 20 bold" ,
239
+ ).grid (row = 3 , column = 4 , columnspan = 2 , padx = 20 , pady = 20 , ipadx = 5 , ipady = 5 )
240
+
241
+ root .mainloop ()
0 commit comments