Amazing-Python-Scripts
180 строк · 5.1 Кб
1from tkinter import *
2
3
4def SetStatusError():
5""" Sets Status bar label to error message """
6Status["text"] = "Wronge Input(s)... :\ "
7Status["fg"] = "red"
8
9
10def Con_Base_X_to_Dec(num, base_x):
11# Converting the integeral part
12# Extract integerals in reverse order
13integeral_part = str(int(num))[::-1]
14i = 0
15res = 0
16for number in integeral_part:
17# Convert each number to decimal
18element = int(number) * (base_x**i)
19# Add element to result
20res += element
21i += 1
22
23# Converting the decimal part
24decimal_part = str(num)
25# Extract decimal part using string manipulation
26decimal_part = decimal_part[decimal_part.index(".")+1:]
27i = -1
28for decimal in decimal_part:
29# Convert each number to decimal
30element = int(decimal) * (base_x**i)
31# Add element to result
32res += element
33i += -1
34
35# Return total result
36return res
37
38
39def Con_Dec_to_Base_Y(num, base_y):
40# Converting the integeral part
41integeral_part = int(num)
42res_int = []
43while int(integeral_part) != 0:
44integeral_part = integeral_part / base_y # Divide number by base
45element = (integeral_part - int(integeral_part)) * \
46base_y # Get the remainder
47# Append element
48res_int.append(str(int(element)))
49# Numbers are organised from LCM to HCM
50res_int = res_int[::-1]
51
52# Converting the decimal part
53decimal_part = num - int(num)
54res_dec = []
55while (decimal_part != 0):
56decimal_part = (decimal_part - int(decimal_part)) * \
57base_y # Multiply decimal part by base
58# Check if not duplicated, for no infinite loops
59if str(int(decimal_part)) in res_dec:
60break
61# Append element
62res_dec.append(str(int(decimal_part)))
63
64# Organize result
65if len(res_dec) == 0:
66# If result has decimal numbers
67res = res_int
68else:
69res = res_int + ["."] + res_dec # If not
70
71# Return grouped result
72return " ".join(res)
73
74
75def Main():
76""" Function to validate entry fields """
77# <----- Validation ----->
78# Validate Number
79num = Num_Val.get()
80try:
81num = float(num)
82except:
83Num.focus()
84SetStatusError()
85return
86# Validate Base X
87base_x = Base_X_Val.get()
88try:
89base_x = int(base_x)
90except:
91Base_X.focus()
92SetStatusError()
93return
94# Validate Base X
95base_y = Base_Y_Val.get()
96try:
97base_y = int(base_y)
98except:
99Base_Y.focus()
100SetStatusError()
101return
102# If same bases are entered
103if base_x == base_y or base_x < 2 or base_y < 2:
104Status["text"] = "Huh?! -_- "
105Status["fg"] = "orange"
106return
107# <----- check base x value ----->
108if base_x == 10:
109Result = Con_Dec_to_Base_Y(num, base_y)
110if base_y == 10:
111Result = Con_Base_X_to_Dec(num, base_x)
112else:
113Result = Con_Base_X_to_Dec(num, base_x)
114Result = Con_Dec_to_Base_Y(Result, base_y)
115
116Status["text"] = "Successfull Conversion! :0 "
117Status["fg"] = "green"
118Result_Entry_Val.set(Result)
119
120
121# <----- GUI Code Beginning ----->
122main_window = Tk()
123main_window.title("Base-N Calculator")
124Icon = PhotoImage(file="./Base-N_Calc/data/GSSOC.png")
125main_window.iconphoto(False, Icon)
126main_window.geometry("420x250")
127
128
129# <----- Elements for number that is going to be converted ----->
130Num_Label = Label(main_window, text="Enter Number :",
131anchor=E, font=("Calibri", 9))
132Num_Label.place(x=30, y=30)
133Num_Val = StringVar()
134Num = Entry(main_window, textvariable=Num_Val, font=("Calibri", 9))
135Num.place(x=120, y=32)
136
137
138# <----- Elements for Base-X ----->
139Base_X_Label = Label(main_window, text="Base-X :",
140anchor=E, font=("Calibri", 9))
141Base_X_Label.place(x=250, y=30)
142Base_X_Val = StringVar()
143Base_X = Entry(main_window, textvariable=Base_X_Val, font=("Calibri", 9))
144Base_X.place(x=305, y=32, width=30)
145
146
147# <----- Elements for Base-Y ----->
148Base_Y_Label = Label(main_window, text="Base-Y :",
149anchor=E, font=("Calibri", 9))
150Base_Y_Label.place(x=250, y=50)
151Base_Y_Val = StringVar()
152Base_Y = Entry(main_window, textvariable=Base_Y_Val, font=("Calibri", 9))
153Base_Y.place(x=305, y=52, width=30)
154
155
156# <----- Elements for calculate button ----->
157Calculate_Button = Button(main_window, text="Convert",
158font=("Calibri", 9), command=Main)
159Calculate_Button.place(x=180, y=75, width=80)
160
161
162# <----- Elements for Result ----->
163Result_Label = Label(main_window, text="Result :",
164anchor=E, font=("Calibri", 9))
165Result_Label.place(x=100, y=130)
166Result_Entry_Val = StringVar()
167Result_Entry = Entry(
168main_window, textvariable=Result_Entry_Val, font=("Calibri", 9))
169Result_Entry.configure(state='readonly')
170Result_Entry.place(x=150, y=130)
171
172
173# <----- Elements for Status Bar ----->
174Status = Label(main_window, text="Hello!! :D", fg="green", font=(
175"Calibri", 9), bd=1, relief=SUNKEN, anchor=W, padx=3)
176Status.pack(side=BOTTOM, fill=X)
177
178
179# <----- Load Main Window ----->
180main_window.mainloop()
181