7. Tkinter - Interaction with humans
part2
After reading Tkinter - The Basics you should be familiar with using Tkinter to make buttons, and you should know how to make Entry boxes and know their properties.
Well now you are ready for another step.
Here is a program that gives you a button and when you press it, something happens(You wont understand some parts of it, dont worry i'll explain everything)
Code:
from Tkinter import *
def hulk():
master = Tk()
root = Tk()
b = Button(root, text = "press me",command = hulk)
b.pack()
root.mainloop()
Ok, Here is what happened. I defined hulk, that means that hulk = do something.
The something that i chose was to make a new window, called master.
Then i did a button on root, with a command to do hulk.The command hulk is to open a new window so a new window was created.
We can also put text, buttons, entrys... on master.
from Tkinter import *
def ice2():
box2 = Tk()
no = Label(box2, text = "why?, i would")
no.pack()
def ice1():
box = Tk()
yes = Label(box, text = "me too")
yes.pack()
def hulk():
master = Tk()
text = Label(master, text ="Would you like some icecream?")
text.pack()
a1=Button(master, text = "yes",fg = "blue", command = ice1)
a1.pack(side = "left")
a2=Button(master, text ="no", fg = "red", command = ice2)
a2.pack(side = "left")
root = Tk()
b = Button(root, text = "press me",command = hulk)
b.pack()
root.mainloop()
Look over this code carefully and make sure you fully understand it.
connecting buttons with Entry boxws
Suppose You want to progeam some kind of a calculator, then you will definatly need to connect buttons and entry boxes.
Before we can go and do that, you'll need to learn some things about entry boxes.
When you have a entry box,
a[0] = float(nameofentrybox.get())
means that a[0] is now whats in the entry box,
nameofentry.delete(0,END) clears the entry box
nameofentry.insert(END, str(blahblahblah) will insert blahblah in the entry box.
Use float when dealing with numbers(exp:3.141593) and use str when dealing with characters(letters:exp:"hello")
Knowing all that you should be able to construct something like this:
That a nice program, you write whatever you want in the entry box and the clear button clears it. It's not hard to make if you read the above. Here is the code:
from Tkinter import *
def hulk():
entry.delete(0,END)
root = Tk()
entry = Entry(root, text = range(0,5), width = 10)
entry.pack()
button = Button(root, text = "clear", command = hulk)
button.pack()
root.mainloop()
We said that hulk equals clear the entry box, the command from the button was to do hulk-therefore when the button is pressed hulk is preformed and the enrty box is cleared.
Now lets learn about buttons.
In order to have a calculator you need to have buttons.
Here is a program that using buttons will insert number in the entry box.
(to do this we use the -- nameofentry.insert(END, str(put text here))
code:
from Tkinter import *
def clear():
entry.delete(0,END)
def p_1():
entry.insert(END, str(1))
root = Tk()
entry = Entry(root, text = range(0,5), width = 14)
button_1 = Button(root, text = "1", fg = "blue", command = p_1)
button = Button(root, text = "clear", command = hulk)
entry.pack()
button_1.pack()
button.pack()
root.mainloop()
To this program we added another command p_1, this command inserts "1" into the entry box. When button_1 is pressed P_1 is preformed.
exs:make a program just like above but with 4 buttons.
Making a sqrt calculator
To make a sqrt(square root) calculator we need to use basic "list".
code:
from Tkinter import *
from math import sqrt
rom =[0]
def hulk():
rom[0] = float(entry.get())
entry.delete(0,END)
entry.insert(END, str(sqrt(rom[0])))
root = Tk()
root.title("s.r.c")
text = Label(root, text = "square root calculator",fg = "red")
entry = Entry(root, width = 14)
b = Button(root, text = "calclulate", command = hulk)
text.pack()
entry.pack()
b.pack()
root.mainloop()
We let "rom" be a list and hulk do the following:
let rom[0] be whatever is written in the entry box
erase everything thats in the entry box
and insert in the entry box the sqrt of rom[0](which is whatever was in the entry box earlier).
Why did i use rom[0] instead of just rom?
Because rom[0] can change whenever you tell it to...but rom will always stay the same.
If you want to learn more about lists visit my list tutorial
I'll add more to this tutorial on the weekend