Amazing-Python-Scripts

Форк
0

README.md

Dice Rolling Simulator

Description

We all know about dice. It’s a simple cube with numbers from 1 to 6 written on its face. But what is simulation? It is making a computer model. Thus, a dice simulator is a simple computer model that can roll a dice for us.


Step 1: Importing the required modules


We will import the following modules:

  • Tkinter: Imported to use Tkinter and make GUI applications.
  • Image, Imagetk: Imported from PIL, i.e. Python Imaging Library. We use it to perform operations involving images in our UI.
  • Random: Imported to generate random numbers.

Step 2: Building a top-level widget to make the main window for our application


In this step, we will build the main window of our application, where the buttons, labels, and images will reside. We also give it a title by title() function.


Step 3: Designing the buttons


Now, just think, what we need to roll a die? Just our hands!

Here, we use pack() to arrange our widgets in row and column form. The ‘BlankLine’ label is to skip a line, whereas we use ‘HeadingLabel’ label to give a heading.

  • root – the name by which we refer to the main window of the application
  • text – text to be displayed in the HeadingLabel
  • fg – the colour of the font used in HeadingLabel
  • bg – background colour of the HeadingLabel
  • font – used to give customised fonts to the HeadingLabel text
  • .pack() – Used to pack the widget onto the root window

Step 4: Forming a list of images to be randomly displayed


‘dice’ is the list of names of images kept in same folder, which are chosen randomly according to the random number generated.

‘DiceImage’ is used to store an image of dice which is chosen by randomly generated numbers.


Step 5: Constructing a label for image, adding a button and assigning functionality


‘ImageLabel’ is to place an image in the window. The parameter expands declared as True so that even if we resize the window, image remains in the center.

Major function:

‘rolling_dice’ function is a function that is executed every time a button is clicked. This is attained through the ‘command=rolling_dice’ parameter while defining a button.


Step 6: Forming a list of images to be randomly displayed


‘root.mainloop()’ is used to open the main window. It acts as the main function of our program.

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.