Python Dice Roll Simulator Project

Python Dice Roll Simulator:

We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.

(1) Open the PyCharm code editor and create a new project in it. We named the project dice_roll_simulator.py. To build a Dice Roll Simulator in Python, we need to install some packages. For this open the terminal in PyCharm and install two packages i.e. tkinter and Pillow. You install all packages at the same time by simply typing in the terminal following command i.e. pip install tk Pillow and then press enter as shown below.

pip install tk pillow python

Note: If the package shows an error while using in the code, you can simply install the package by clicking on the red bulb which appears below the error and you can directly install the package from here.

(4) Now add some images in the path where you have created the project as shown below.

dice roll images for python

Download Images from Here: Dice Roll Project Images.

(3) Write the following code for creating Python Dice Roll Simulator Project as shown below.

Python Dice Roll Simulator Project Code 1
Python Dice Roll Simulator Project Code 2

Python Dice Roll Simulator Project Source Code:

import tkinter
from tkinter import *
from PIL import Image, ImageTk
import random

drs=Tk()
drs.title("Dice Roll Simulator")
drs.geometry("600x400")
drs.resizable(False,False)
drs.config(bg="#CCE3DE")

dice_img=["one.png","two.png","three.png","four.png","five.png","six.png"]
img1=ImageTk.PhotoImage(Image.open(random.choice(dice_img)))
img2=ImageTk.PhotoImage(Image.open(random.choice(dice_img)))

label1=tkinter.Label(drs,image=img1)
label2=tkinter.Label(drs,image=img2)

label1.image=img1
label2.image=img2

label1.place(x=40,y=100)
label2.place(x=340,y=100)

def dice_roll():
    img1 = ImageTk.PhotoImage(Image.open(random.choice(dice_img)))
    label1.config(image=img1)
    label1.image=img1

    img2 = ImageTk.PhotoImage(Image.open(random.choice(dice_img)))
    label2.config(image=img2)
    label2.image = img2

btn=tkinter.Button(drs,text="Dice Roll",bg="red",fg="white",font=("Merriweather",22,"bold"),command=dice_roll)
btn.place(x=240,y=0)

drs.mainloop()

(4) Now run the code and a window pop up, click on the Dice Roll button to roll your dice. The output window will be shown below.

Python Dice Roll Simulator Project Output Window

Tutorial MySQL
Tutorial Python
Procedure-Oriented Programming
Modular Programming
Object-Oriented Programming (OOP)
C++ Character Set and Program Structure
FOR Loop in C Language
Python (programming language)– Wikipedia

Comments (No)

Leave a Reply