There are various third-party libraries in Python to design graphical user interfaces. Tkinter is the most popular, common, and easy-to-use library for building GUI graphical user interface applications.
Tkinter offers a powerful object-oriented interface and is easy to use. In this article, we'll use Tkinter and Random to create a GUI application that simulates the roll of dice.
from tkinter import *
import random
root=tk()
root.geometry("800x400")
root.title('Roll the dice')
l1=label(root,font=("helvetica",260))
def roll():
dice=['\u2680','\u2681','\u2682','\u2683','\u2684','\u2685']
l1.config(text=f'', fg="red")
l1.pack()
b1=button(root,text="Roll the dice",foreground='blue',command=roll)
b1.place(x=300,y=0)
b1.pack()
root.mainloop()
The operation effect is shown in the following figure.
Import the required libraries.
from tkinter import *
import random
Create an application window and set the size of the window to 800*400. Set the window title: Roll the dice. Create a label label L1.
root=tk()
root.geometry("800x400")
root.title('Roll the dice')
l1=label(root,font=("helvetica",260))
Create a custom function, roll(), to simulate a roll of the dice.
Create a list of dice where you store 1 to 6 dice corresponding to ASCII characters.
Use the random module's choices() method to randomly select an element three times from the list. Store the results in label L1.
def roll():
dice=['\u2680','\u2681','\u2682','\u2683','\u2684','\u2685']
l1.config(text=f'', fg="red")
l1.pack()
Add a button to execute the function rool.
b1=button(root,text="Roll the dice",foreground='blue',command=roll)
b1.place(x=300,y=0)
b1.pack()
root.mainloop()
It's not easy to create an article, if you like this article, please follow, like and share it with your friends. If you have comments and suggestions, please give us feedback in the comments.