defaddBase():# the addBase function will add whatever base is in the baseInput entry box with the button is pressed
out=[]
forbaseinbaseList['values']:# create a list that is a copy of the base list, but as integers instead of strings (so it can be sorted)
forbaseinbaseDropdown['values']:# create a list that is a copy of the base list, but as integers instead of strings (so it can be sorted)
out.append(int(base))
try:
ifnotint(baseInput.get())inout:# if the base isn't already there,
out.append(int(baseInput.get()))# then add it to the list
baseList['values']=sorted(out)# apply the list to the dropdown, after sorting it
baseDropdown['values']=sorted(out)# apply the list to the dropdown, after sorting it
functionTrigger(0,0,0)# Trigger number conversion
exceptValueError:# error is raised if the base is not a number
exceptValueError:# error is caught if the base is not a number
displayMessage("Not a valid base!")
# functionTrigger is ran when the number input box is changed, or when a base is added
deffunctionTrigger(a,b,c):# Positional arguments are not used for this function to run, but still must be defined to allow the finction to be called by a tracer
input=numberInput.get()# Retreive the input number
base=int(baseList['values'][baseList.current()])# Retreive the current selected base
baseConverter(input,base)# run the conversion with the selected base and input
base=int(baseDropdown['values'][baseDropdown.current()])# Retreive the current selected base
listOut.delete(0,listOut.size())# clear the output
output=baseConverter(input,base)# run the conversion with the selected base and input
forlineinoutput:# insert each line into the output listbox at the end
listOut.insert(tk.END,line)
# displayMessage will simply write a message to the output listbox.
defdisplayMessage(message):
@ -37,13 +41,12 @@ def displayMessage(message):
defbaseConverter(userInput,numberBase):# arguments: userInput: the user's number to be converted. numberBase: the number base of the input
try:
decimalNum=int(userInput,numberBase)# First, try to get the number in decimal.
decimalNum=int(userInput,numberBase)# first, try to get the number in decimal.
except:# If this fails, then the input from the user does not work in this base, or might not be a number at all
displayMessage("Number entered does not match the base!")
return
listOut.delete(0,listOut.size())# clear the output
forbaseinbaseList['values']:# Iterate through all bases
outputList=[]
forbaseinbaseDropdown['values']:# iterate through all bases
outputText="Base "+base+": "# prepare a string for the line
outputNumber=""
tempNum=decimalNum# create a temp number to modify
@ -57,8 +60,9 @@ def baseConverter(userInput, numberBase): # arguments: userInput: the user's num
outputNumber+=chr(55+digit)# ASCII character offset for capital letters
outputNumber=outputNumber[::-1]# reverse the output number, as the above loop added digits in reverse
outputText+=outputNumber
listOut.insert(tk.END,outputText)# insert the line into the output listbox at the end
outputText+=outputNumber#add the number to the rest of the string
outputList.append(outputText)# add each conversion to the output list