INTRODUCTION
In our second app we are going to make a very useful Hexadecimal, decimal and binary converter. Although incredibly useful for Computer Science teachers you will be able to adapt the code for any convertor that you wish!
PREREQUISITES
You should not attempt this tutorial until you are fully familiar with all the IGCSE tutorials. You need to be comfortable with if, elif, try, except, simple string manipulation and variables. You should also have a go at the first GUI Tutorial.
SETUP
Introduction:
import tkinter window = tkinter.Tk()
These lines are always used in tkinter to get it started.
window.wm_title("Bin / Hex / Dec")
This line gives the window a title.
Base_Number=""
This sets up the variable Base_Number because of the radio button it would sometimes crash if you did not choose a button before trying to do a calculation.
EVALUATE FUNCTION
This part is the actual code that does the calculations.
def evaluate(event):
if Base_Number == "Binary":
try:
dec = int(Myentry.get(),2)
myhex = hex(dec)
result1.configure(text = "Decimal is: "+str(dec))
result2.configure(text = "Hex is: "+str(myhex))
except ValueError:
result1.configure(text = "Please enter valid binary")
result2.configure(text = "")
if Base_Number == "Binary":
This checks to see that the radio button has been set to binary.
try:
We then use a try and except to deal with somebody accidentally entering a value that is not in binary. i.e. not 1s or 0s.
dec = int(Myentry.get(),2)
myhex = hex(dec)
These two lines do the actual calculation in Python. You can do any calculation here that you want!
result1.configure(text = "Decimal is: "+str(dec))
result2.configure(text = "Hex is: "+str(myhex))
Finally we print the two results. Note that tkinter will only accept strings in the text lines. To do this we configure the existing lines of text result1 and result2
The other two Base_Numbers work in a similar way and the final else deals with if a radio button has not been set.
CALCSTYLE FUNCTION
This function sets the Base_Number and makes it accessible for other functions. The print statement is optional and displays in the Python Window.
LAYING OUT THE WINDOW
MyTitle = tkinter.Label(window, text="Bin / Hex / Dec Converter")
MyTitle.pack()
This is simple text to say it’s a calculator
Myentry = tkinter.Entry(window)
Myentry.bind("<Return>", evaluate)
Myentry.pack()
This is a text entry window that runs the function evaluate when return is pressed.
result1 = tkinter.Label(window, text="1. Choose a base")
result1.pack()
result2 = tkinter.Label(window, text="2. Enter a number and press<enter>")
result2.pack()
These two lines of text are initially the instructions, but change to the results once a number has been entered.
base = tkinter.StringVar()
tkinter.Radiobutton(window, text="Binary", variable=base, value="Binary", command=calcStyle).pack()
tkinter.Radiobutton(window, text="Decimal", variable=base, value="Decimal", command=calcStyle).pack()
tkinter.Radiobutton(window, text="Hex", variable=base, value="Hex", command=calcStyle).pack()
These are the radio buttons. You can have as many as you like, but they must use the same variable.
window.mainloop()
Finally the command to make the loop go round.
In our second app we are going to make a very useful Hexadecimal, decimal and binary converter. Although incredibly useful for Computer Science teachers you will be able to adapt the code for any convertor that you wish!
PREREQUISITES
You should not attempt this tutorial until you are fully familiar with all the IGCSE tutorials. You need to be comfortable with if, elif, try, except, simple string manipulation and variables. You should also have a go at the first GUI Tutorial.
SETUP
Introduction:
import tkinter window = tkinter.Tk()
These lines are always used in tkinter to get it started.
window.wm_title("Bin / Hex / Dec")
This line gives the window a title.
Base_Number=""
This sets up the variable Base_Number because of the radio button it would sometimes crash if you did not choose a button before trying to do a calculation.
EVALUATE FUNCTION
This part is the actual code that does the calculations.
def evaluate(event):
if Base_Number == "Binary":
try:
dec = int(Myentry.get(),2)
myhex = hex(dec)
result1.configure(text = "Decimal is: "+str(dec))
result2.configure(text = "Hex is: "+str(myhex))
except ValueError:
result1.configure(text = "Please enter valid binary")
result2.configure(text = "")
if Base_Number == "Binary":
This checks to see that the radio button has been set to binary.
try:
We then use a try and except to deal with somebody accidentally entering a value that is not in binary. i.e. not 1s or 0s.
dec = int(Myentry.get(),2)
myhex = hex(dec)
These two lines do the actual calculation in Python. You can do any calculation here that you want!
result1.configure(text = "Decimal is: "+str(dec))
result2.configure(text = "Hex is: "+str(myhex))
Finally we print the two results. Note that tkinter will only accept strings in the text lines. To do this we configure the existing lines of text result1 and result2
The other two Base_Numbers work in a similar way and the final else deals with if a radio button has not been set.
CALCSTYLE FUNCTION
This function sets the Base_Number and makes it accessible for other functions. The print statement is optional and displays in the Python Window.
LAYING OUT THE WINDOW
MyTitle = tkinter.Label(window, text="Bin / Hex / Dec Converter")
MyTitle.pack()
This is simple text to say it’s a calculator
Myentry = tkinter.Entry(window)
Myentry.bind("<Return>", evaluate)
Myentry.pack()
This is a text entry window that runs the function evaluate when return is pressed.
result1 = tkinter.Label(window, text="1. Choose a base")
result1.pack()
result2 = tkinter.Label(window, text="2. Enter a number and press<enter>")
result2.pack()
These two lines of text are initially the instructions, but change to the results once a number has been entered.
base = tkinter.StringVar()
tkinter.Radiobutton(window, text="Binary", variable=base, value="Binary", command=calcStyle).pack()
tkinter.Radiobutton(window, text="Decimal", variable=base, value="Decimal", command=calcStyle).pack()
tkinter.Radiobutton(window, text="Hex", variable=base, value="Hex", command=calcStyle).pack()
These are the radio buttons. You can have as many as you like, but they must use the same variable.
window.mainloop()
Finally the command to make the loop go round.
Final code
EXERCISE 1:
Try and do a different calculator. For example you could do a Miles to Kilometres conversion or Physics conversions.
Try and do a different calculator. For example you could do a Miles to Kilometres conversion or Physics conversions.