Hello Tkinter coders! Today we are going to learn to build our own random mobile number generator. The application can be really helpful for developers who need some fake data to work on.
Creating a Random Mobile Number Generator in Python
Let’s get right into the steps to create our random number generator using Tkinter. It’s a fun little application that you’ll enjoy working on and somethign you can show off in your beginner portfolios too!
Step 1: Importing Modules and Creating a Tkinter Window
The first step in any Tkinter project is to import the tkinter
and random
modules and then create a blank window to work on. We can configure the window according to our own preferences.
We will be providing the window with a title and background color. Along with this we will set the resize attributes of the window as false
.
import tkinter as tk
window = tk.Tk()
window.geometry("600x200")
window.config(bg="#F39C12")
window.resizable(width=False,height=False)
window.title('Random Mobile Number Generator')
window.mainloop()

Step 2: Adding Widgets on the Window Created
The step involves addition of various widgets on the window that we just created which include labels and buttons. We will also create a blank label which will be set to the mobile number generated in the later sections.
For each widget, we will be creating the widget variable and then place them on the screen with the help of the place function which requires the x and y coordinates of the widget.
import tkinter as tk
window = tk.Tk()
window.geometry("600x200")
window.config(bg="#F39C12")
window.resizable(width=False,height=False)
window.title('Random Mobile Number Generator')
l1 = tk.Label(text="Random Mobile Number Generator",font=("Arial",20),bg="Black",fg="White")
b1 = tk.Button(text="Click on me to generate a mobile number",font=("Arial",15),bg="#A3E4D7")
l2 = tk.Label(bg="#F39C12",font=("Arial",30),text="")
l1.place(x=100,y=20)
b1.place(x=110,y=70)
l2.place(x=165,y=130)
window.mainloop()

Step 3: Creating a function to generate a random number
Now coming to creating a function to generate the random number we will be making use of the random
module and then create a list that contains all the digits from 0-9 in form of strings stored together.
We would initiate a number variable as an empty string and choose 10 random digits from the list and keep appending them into the number variable. Lastly, we would set the text of the empty string as the number generated.
After that don’t forget to add the function name in the button declaration as the command
property. And you are all set!
import random
def generate_number():
list = ["0","1","2","3","4","5","6","7","8","9"]
number = ""
for i in range(10):
number = number + random.choice(list)
l2.config(text = number)
The Final Code
import random
def generate_number():
list = ["0","1","2","3","4","5","6","7","8","9"]
number = ""
for i in range(10):
number = number + random.choice(list)
l2.config(text = number)
import tkinter as tk
window = tk.Tk()
window.geometry("600x200")
window.config(bg="#F39C12")
window.resizable(width=False,height=False)
window.title('Random Mobile Number Generator')
l1 = tk.Label(text="Random Mobile Number Generator",font=("Arial",20),bg="Black",fg="White")
b1 = tk.Button(text="Click on me to generate a mobile number",font=("Arial",15),bg="#A3E4D7",command=generate_number)
l2 = tk.Label(bg="#F39C12",font=("Arial",30),text="")
l1.place(x=100,y=20)
b1.place(x=110,y=70)
l2.place(x=165,y=130)
window.mainloop()
Some Sample Outputs


Conclusion
Congratulations! Today we learned how to generate mobile numbers using python tkinter technology! Try it out yourself!
Thank you for reading! Stay tuned to learn more!