Chapter 10 : Programming visually

Example 10.1, Page No 171

In [5]:
import random
nums = range(50)
for i in range(1,50):
    nums[i] = i
for i in range(1,50):
    j = random.randrange(1,49, 1)
    k = nums[i]
    nums[i] = nums[j]
    nums[j] = k
print "Your six lucky numbers are: "
for i in range(1,7):
    print nums[i]," ",
    
Your six lucky numbers are: 
30   5   38   20   7   29  

Example 10.2, Page No 173

In [1]:
from Tkinter import *
from PIL import Image, ImageTk
import random

def getBtnClick():
    nums = range(50)
    for i in range(1,50):
        nums[i] = i
    for i in range(1,50):
        j = random.randrange(1,49, 1)
        k = nums[i]
        nums[i] = nums[j]
        nums[j] = k
    label1.config(text = nums[0])
    label2.config(text = nums[1])
    label3.config(text = nums[2])
    label4.config(text = nums[3])
    label5.config(text = nums[4])
    label6.config(text = nums[5])
    getBtn.config(state=DISABLED)
    resetBtn.config(state=NORMAL)
def resetBtnClick():
    label1.config(text = "...")
    label2.config(text = "...")
    label3.config(text = "...")
    label4.config(text = "...")
    label5.config(text = "...")
    label6.config(text = "...")
    resetBtn.config(state=DISABLED)
    getBtn.config(state=NORMAL)
root = Tk()
root.title("Lotto number generator")
image = Image.open("lotto.gif")
photo = ImageTk.PhotoImage(image)
label0 = Label(image=photo)
label0.image = photo
label0.grid(row=0,column=0,rowspan=2)

label1 = Label(text="...")
label1.grid(row=0,column=1,padx=5,pady=5)
label2 = Label(text="...")
label2.grid(row=0,column=2,padx=5,pady=5)
label3 = Label(text="...")
label3.grid(row=0,column=3,padx=5,pady=5)
label4 = Label(text="...")
label4.grid(row=0,column=4,padx=5,pady=5)
label5 = Label(text="...")
label5.grid(row=0,column=5,padx=5,pady=5)
label6 = Label(text="...")
label6.grid(row=0,column=6,padx=5,pady=5)

getBtn = Button(text="Get My Lucky Numbers",command=getBtnClick)
getBtn.grid(row=1,column=2,columnspan=2)
resetBtn = Button(text="Reset",state=DISABLED,command=resetBtnClick)
resetBtn.grid(row=1,column=4,columnspan=3)
root.mainloop()