Housie random number generator


Youtube Live session on Tkinter

Tkinter Projects in Tkinter

Housie game random numbers

  1. Lowest number is 1 and highest number is 90
  2. Numbers already called are to be displayed
  3. Just previous number of Call is to be displayed
  4. One time use random ( sequence ) numbers

Generating Numbers

list1=random.sample(range(1,91), 90) 
my_iter=iter(list1) # creating iterator
One list is prepared by using unique 90 numbers in random sequence starting from 1 to 90 ( inclusive ). Using this list list1, one iterator is prepared.
Random Number Generator

Displaying grid of numbers

Numbers from 1 to 90 will be displayed in 10 rows. Once a number is called ( used ) the same number font and background colour will be changed to mark that the number is already taken out of the list. This function display_no() is called at the starting to display the grid.
def display_no(): # display all buttons at starting
    row=3
    col=2
    for i in range(1,91):
        btn = tk.Button(my_w, text=i,font=font2,
         bg='#ffffff',fg='#C0c0c0')
        btn.grid(row=row, column=col,padx=2)
        if(col==11):  # one row is complete, move next 
            col=2     # staring column 
            row=row+1 # next row 
        else:
            col=col+1 # move to next column ( same row ) 
        buttons.append(btn) # keep the button reference 

Call for Number

On Click of button ( b1 ) the function my_next() is triggered. Inside this function we are using one try except code block to collect one number ( on each call ) and change the respective button background colour and font colour to mark as used ( already called )
def my_next(): # Click of Next button 
    global s_no2
    #print(s_no2)
    str2.set(str(s_no2)) # set the previous call number
    try:
        s_no=next(my_iter) # Get the call number 
        buttons[s_no-1].config(bg="lightgreen",fg='#000000')
        str1.set(str(s_no))
        s_no2=s_no  # store to use as previous call number 
    except StopIteration: # No more number available
        str1.set('0')
        print ("This is a StopIteration error")
Full code is here.
import random
list1=random.sample(range(1,91), 90) 
my_iter=iter(list1) # creating iterator
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("410x500")  # Size of the window 
my_w.title('www.plus2net.com')

font1=('times', 64, 'bold')  # font style of Last call 
font2=('times', 14, 'normal') # font style of grid numbers
font3=('times', 20, 'bold')  # font style of the button

b1=tk.Button(my_w,text='Next',font=font3,command=lambda:my_next())
b1.grid(row=1,column=1,columnspan=5) # show the button to click

str1=tk.StringVar() # Last call to display 
str2=tk.StringVar() # Previous to Last call 
str1.set(0)  # Initial value
str2.set(0)  # Initial value
l1=tk.Label(my_w,textvariable=str1,font=font1) # Show the call
l1.grid(row=1,column=5,columnspan=3,padx=10) 
l2=tk.Label(my_w,textvariable=str2) # show previous to call
l2.grid(row=1,column=9,padx=10)

buttons=[] # to store reference to buttons

def display_no(): # display all buttons at starting
    row=3
    col=2
    for i in range(1,91):
        btn = tk.Button(my_w, text=i,font=font2,
         bg='#ffffff',fg='#C0c0c0')
        btn.grid(row=row, column=col,padx=2)
        if(col==11):  # one row is complete, move next 
            col=2     # staring column 
            row=row+1 # next row 
        else:
            col=col+1 # move to next column ( same row ) 
        buttons.append(btn) # keep the button reference 
global s_no2
s_no2=0

def my_next(): # Click of Next button 
    global s_no2
    #print(s_no2)
    str2.set(str(s_no2)) # set the previous call number
    try:
        s_no=next(my_iter) # Get the call number
        buttons[s_no-1].config(bg="lightgreen",fg='#000000')
        str1.set(str(s_no)) 
        s_no2=s_no # store to use as previous call number
    except StopIteration: # No more number available
        str1.set('0')
        print ("This is a StopIteration error")
    
display_no() # show all the numbers in layout

copyright_symbol = u"\u00A9"
l1_end=tk.Label(my_w,text=copyright_symbol) # footer messages
l1_end.grid(row=14,column=1,padx=4)
l2_end=tk.Label(my_w,text='www.plus2net.com') #footer
l2_end.grid(row=14,column=2,columnspan=5,padx=20,pady=10)

my_w.mainloop()  # Keep the window open
Python Tkinter Projects
Tkinter Text Tkinter Entry How to Validate user entered data
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer