defaddBase():# the addBase function will add whatever base is in the baseInput entry box with the button is pressed
defaddBase():# the addBase function will add whatever base is in the baseInput entry box with the button is pressed
out=[]
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))
out.append(int(base))
try:
try:
ifnotint(baseInput.get())inout:# if the base isn't already there,
ifnotint(baseInput.get())inout:# if the base isn't already there,
out.append(int(baseInput.get()))# then add it to the list
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
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!")
displayMessage("Not a valid base!")
# functionTrigger is ran when the number input box is changed, or when a base is added
# 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
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
input=numberInput.get()# Retreive the input number
base=int(baseList['values'][baseList.current()])# Retreive the current selected base
base=int(baseDropdown['values'][baseDropdown.current()])# Retreive the current selected base
baseConverter(input,base)# run the conversion with the selected base and input
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.
# displayMessage will simply write a message to the output listbox.
defdisplayMessage(message):
defdisplayMessage(message):
listOut.delete(0,listOut.size())# delete all existing entries
listOut.delete(0,listOut.size())# delete all existing entries
@ -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
defbaseConverter(userInput,numberBase):# arguments: userInput: the user's number to be converted. numberBase: the number base of the input
try:
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
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!")
displayMessage("Number entered does not match the base!")
return
return
outputList=[]
listOut.delete(0,listOut.size())# clear the output
forbaseinbaseDropdown['values']:# iterate through all bases
forbaseinbaseList['values']:# Iterate through all bases
outputText="Base "+base+": "# prepare a string for the line
outputText="Base "+base+": "# prepare a string for the line
outputNumber=""
outputNumber=""
tempNum=decimalNum# create a temp number to modify
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+=chr(55+digit)# ASCII character offset for capital letters
outputNumber=outputNumber[::-1]# reverse the output number, as the above loop added digits in reverse
outputNumber=outputNumber[::-1]# reverse the output number, as the above loop added digits in reverse
outputText+=outputNumber
outputText+=outputNumber#add the number to the rest of the string
listOut.insert(tk.END,outputText)# insert the line into the output listbox at the end
outputList.append(outputText)# add each conversion to the output list