some comment tweaks
This commit is contained in:
parent
d4b205c7c4
commit
f27bd270c2
36
main.py
36
main.py
@ -1,33 +1,37 @@
|
|||||||
import tkinter as tk # using tkinter for the GUI
|
import tkinter as tk # using tkinter for the GUI
|
||||||
import tkinter.ttk as ttk
|
import tkinter.ttk as ttk
|
||||||
|
# Requirements to run: python3 pytk
|
||||||
|
|
||||||
class Window(tk.Frame): # create a tkinter frame
|
class Window(tk.Frame): # create a tkinter frame
|
||||||
def __init__(self, master=None):
|
def __init__(self, master=None):
|
||||||
tk.Frame.__init__(self, master)
|
tk.Frame.__init__(self, master)
|
||||||
self.master = master
|
self.master = master
|
||||||
|
|
||||||
root = tk.Tk() # some definitions for the GUi
|
root = tk.Tk() # some definitions for the gui
|
||||||
app = Window(root)
|
app = Window(root)
|
||||||
|
|
||||||
##### Functions #####
|
##### Functions #####
|
||||||
|
|
||||||
def addBase(): # the addBase function will add whatever base is in the baseInput entry box with the button is pressed
|
def addBase(): # the addBase function will add whatever base is in the baseInput entry box with the button is pressed
|
||||||
out = []
|
out = []
|
||||||
for base in baseList['values']: # create a list that is a copy of the base list, but as integers instead of strings (so it can be sorted)
|
for base in baseDropdown['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:
|
||||||
if not int(baseInput.get()) in out: # if the base isn't already there,
|
if not int(baseInput.get()) in out: # 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
|
||||||
except ValueError: # error is raised if the base is not a number
|
except ValueError: # 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
|
||||||
def functionTrigger(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
|
def functionTrigger(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
|
||||||
|
for line in output: # 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.
|
||||||
def displayMessage(message):
|
def displayMessage(message):
|
||||||
@ -37,13 +41,12 @@ def displayMessage(message):
|
|||||||
|
|
||||||
def baseConverter(userInput, numberBase): # arguments: userInput: the user's number to be converted. numberBase: the number base of the input
|
def baseConverter(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
|
for base in baseDropdown['values']: # iterate through all bases
|
||||||
for base in baseList['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
|
||||||
|
return outputList # return the list of conversions
|
||||||
|
|
||||||
##### Variables #####
|
##### Variables #####
|
||||||
|
|
||||||
@ -90,10 +94,10 @@ separator2 = ttk.Separator(root, orient='horizontal')
|
|||||||
separator2.place(relx=0, rely=0.43, relwidth=1, relheight=1)
|
separator2.place(relx=0, rely=0.43, relwidth=1, relheight=1)
|
||||||
|
|
||||||
# the dropdown list of bases
|
# the dropdown list of bases
|
||||||
baseList = ttk.Combobox(root, width='10')
|
baseDropdown = ttk.Combobox(root, width='10')
|
||||||
baseList['values']= (2, 10, 16)
|
baseDropdown['values']= (2, 10, 16)
|
||||||
baseList.current(1) #set the default item to base 10
|
baseDropdown.current(1) #set the default item to base 10
|
||||||
baseList.grid(column=1, row=4)
|
baseDropdown.grid(column=1, row=4)
|
||||||
|
|
||||||
# label for the base dropdown
|
# label for the base dropdown
|
||||||
lbl3 = tk.Label(root, text="\nNumber Base \n of the input:\n", font=("Arial", 12))
|
lbl3 = tk.Label(root, text="\nNumber Base \n of the input:\n", font=("Arial", 12))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user