Clean up, add comments, get conversion working

master
Cole Deck 4 years ago
parent 1f32849f33
commit 8ff15efb2d

@ -15,22 +15,61 @@ def add_base():
for x in baseDropdown['values']:
out.append(int(x))
try:
if out.index(int(baseInput.get())) == 0:
if not int(baseInput.get()) in out:
out.append(int(baseInput.get()))
baseDropdown['values'] = sorted(out)
functionTrigger(0,0,0) # Trigger number conversion if a base is added
except ValueError:
print("Not a valid base!")
lbl6 = Label(root, text="\nInvalid base!", font=("Arial", 12))
lbl6.grid(column=1, row=0)
lbl1 = Label(root, text="\nInvalid base!", font=("Arial", 12))
lbl1.grid(column=1, row=0)
def functionTrigger(a,b,c): # Positional arguments from the trace are not needed for this program, but still must be defined
input = numberInput.get()
base = int(baseDropdown['values'][baseDropdown.current()]) # Retreive the current selected base
baseConverter(input, base)
def baseConverter(userInput, numberBase):
try:
decimalNum = int(userInput,numberBase) # First attempt to get the number in decimal.
except:
print("bad base")
return
outputText = ""
for base in baseDropdown['values']: # Iterate through all bases
outputText += "Base " + base + ": "
outputNumber = ""
tempNum = decimalNum
while tempNum > 0:
digit = int(tempNum % int(base))
tempNum = int(tempNum / int(base))
if digit < 10: # Can be represented with 0-9
outputNumber += str(digit)
else: # Alphanumeric characters needed
outputNumber += chr(55 + digit) # ASCII character offset for capital letters
outputNumber = outputNumber[::-1]
outputText += outputNumber + "\n"
lblOut = Label(root, text=outputText, font=("Arial", 12))
lblOut.grid(column=0, row=7)
# set window title
root.wm_title("Numerical converter")
lbl6 = Label(root, text="\n", font=("Arial", 12))
lbl6.grid(column=1, row=0)
lbl0 = Label(root, text="Add a base:", font=("Arial", 12))
lbl0.grid(column=0, row=1)
lbl1 = Label(root, text="\n", font=("Arial", 12))
lbl1.grid(column=1, row=0)
lbl2 = Label(root, text="Add a base:", font=("Arial", 12))
lbl2.grid(column=0, row=1)
addBaseBtn = Button(root, text="Add", command=add_base)
addBaseBtn.grid(column=2, row=1)
@ -39,8 +78,8 @@ addBaseBtn.grid(column=2, row=1)
baseInput = Entry(root,width=10, state='enabled')
baseInput.grid(column = 1, row = 1)
lbl5 = Label(root, text="", font=("Arial", 12))
lbl5.grid(column=0, row=3)
lbl2 = Label(root, text="", font=("Arial", 12))
lbl2.grid(column=0, row=3)
separator1 = Separator(root, orient='horizontal')
separator1.place(relx=0, rely=0.2, relwidth=1, relheight=1)
@ -56,16 +95,21 @@ baseDropdown.grid(column=1, row=4)
lbl3 = Label(root, text="\nNumber Base \n of the input:\n", font=("Arial", 12))
lbl3.grid(column=0, row=4)
userInputRaw = StringVar()
userInputRaw.trace_add("write", functionTrigger)
numberInput = Entry(root,width=10, state='enabled')
numberInput = Entry(root,width=10, state='enabled', textvariable=userInputRaw)
numberInput.grid(column=1, row = 6)
lbl4 = Label(root, text="Number to convert:", font=("Arial", 12))
lbl4.grid(column=0, row=6)
lbl7 = Label(root, text="", font=("Arial", 12))
lbl7.grid(column=50, row=5)
lbl5 = Label(root, text="", font=("Arial", 12))
lbl5.grid(column=50, row=5)
lblOut = Label(root, text="rtedgh", font=("Arial", 12))
lblOut.grid(column=0, row=7)

Loading…
Cancel
Save