Dice Simulator

Dice Simulator Using Python Tkinter

Python

Dice Simulator Using Python in Hindi – In This Tutorial, I Will Explain to You How To Build Dice Simulator In Python With Source Code.

If You Don’t Know About Python Tkinter Library Then Click Here To Learn – Python Tkinter

Dice Simulator Using Python Tkinter

Modules you need for this program.

  1. Tkinter (For GUI window)
  2. Random (For generating random numbers from 1-6)
from tkinter import *
import random

Creating a plane GUI(Graphical User Interface) window with the help of tkinter module.

For creating a plane GUI window you just have to type two lines of code. We will do all the coding in between of these two lines of code so that our code is shown in the GUI window.

from tkinter import *
import random

window = Tk()

window.mainloop()
This is the output of this code.

Giving size and title to our GUI window.

For giving size to our GUI window we will use:-

  1. window.minsize(width, height) – This is for setting the minimum width and height for our window
  2. window.maxsize(width, height) – This is for setting the maximum width and height for our window
  3. window.title(‘Title here’) – This is for giving title to our window
from tkinter import *
import random

window = Tk()

window.minsize(600,600)   # for setting the minimum width and height for our window
window.maxsize(600,600)   # for setting the maximum width and height for our window  
window.title('ROLL THE DICE')  # for giving title to our window

window.mainloop()
We have got a perfect size and title to the window

Giving a heading to our window.

For creating a heading for our window we have to create a label and then inserting the text in that label. Label is a widget which is just for show and user can not interact with it.

inside Label widget we have to give the following arguments :- 
1.  text (For the text shown in that label)
2. font (For adding font to the text in that label)
3. bg(For adding a backgroudcolor to the label )
from tkinter import *
import random

window = Tk()

window.minsize(600,600)   # for setting the minimum width and height for our window
window.maxsize(600,600)   # for setting the maximum width and height for our window  
window.title('ROLL THE DICE')  # for giving title to our window

heading = Label(window,text='ROLL THE DICE',font=('bold',50),bg='light cyan')  # This will creat a label for the heading.
heading.pack(fill=X)   # You have to pack this label so that it can be seen in the window

window.mainloop()
Now we have a heading inside our window

Creating a blank function.
We will create a function and will type “pass” in that function so that we get a blank function. This has nothing inside it but it will not give an error while running it.

from tkinter import *
import random

window = Tk()

def roll():   # This is a blank function
    pass

window.minsize(600,600)   # for setting the minimum width and height for our window
window.maxsize(600,600)   # for setting the maximum width and height for our window  
window.title('ROLL THE DICE')  # for giving title to our window

# This will create a label which will contain heading
heading = Label(window,text='ROLL THE DICE',font=('bold',50),bg='light cyan')
heading.pack(fill=X)

window.mainloop()

Creating a button for our window.

For creating a button we will our Button tag.
Inside the button tag we will give the following arguments:-
1. text(For the text shown in that button)
2. font(For adding a font to the text inside that button)
3. command(In this argument we have to give the name of the function which starts running when we click the button)

from tkinter import *
import random

window = Tk()

def roll():   # This is a blank function
    pass

window.minsize(600,600)   # for setting the minimum width and height for our window
window.maxsize(600,600)   # for setting the maximum width and height for our window  
window.title('ROLL THE DICE')  # for giving title to our window

# This will create a label which will contain heading
heading = Label(window,text='ROLL THE DICE',font=('bold',50),bg='light cyan')
heading.pack(fill=X)

# This will create a button.
button = Button(window,text='Roll',font=('normal',25),command=lambda:roll())
button.pack()

window.mainloop()
We have a button.

Writing the code for generating random numbers from 1-6 inside the roll() function.

For this function first we will make a label outside the function. This is very important to make the label outside the roll() function as if we make the label inside the function when ever the function will run a new label will be made which we don’t want. We want that new number is ti be shown in that same label every time.After this we will make a list which will have all the special code for the numbers from 1-6.The code (\u2680) is for the number 1. you can see all the code for all numbers in the code given below. After this we will write the code which will choose a random number between 1-6 and will add that number into the label.

Now our dice simulator is ready.

from tkinter import *
import random

window = Tk()

label = Label(window,font=('bold',400))
def roll():
    number = ['\u2680','\u2681','\u2682','\u2683','\u2684','\u2685']
    label.config(text=f'{random.choice(number)}')
    label.pack()

window.minsize(600,600)   # for setting the minimum width and height for our window
window.maxsize(600,600)   # for setting the maximum width and height for our window  
window.title('ROLL THE DICE')  # for giving title to our window

# This will create a label which will contain heading
heading = Label(window,text='ROLL THE DICE',font=('bold',50),bg='light cyan')
heading.pack(fill=X)

# This will create a button.
button = Button(window,text='Roll',font=('normal',25),command=lambda:roll())
button.pack()

window.mainloop()
Final look of our program

2 thoughts on “Dice Simulator Using Python Tkinter

Leave a Reply

Your email address will not be published. Required fields are marked *