Random Number Generator using Tkinter: A Step-by-Step Guide with Code

Random Number Generator using Tkinter: A Step-by-Step Guide with Code

Random Number Generator using Tkinter: A Step-by-Step Guide with Code
Random Number Generator using Tkinter: A Step-by-Step Guide with Code

Introduction

In this tutorial, we will be creating a simple random number generator using Tkinter, the built-in Python library for creating graphical user interfaces (GUIs). This tutorial will guide you through the process step-by-step, starting with importing Tkinter and creating the main window, and finishing with running the application.

Importing Tkinter

The first step in creating our random number generator is to import the Tkinter library. We can do this by using the following line of code:

import tkinter as tk

Adding a Label and Entry Widget

Now we will add a label and entry widget to our window. The label will be used to display the message “Enter the range:” and the entry widget will be used to accept input from the user. We will use the grid layout manager to place the label and entry widget on the window.

label = tk.Label(root, text = "Enter the range:")
label.grid(row = 0, column = 0)

entry = tk.Entry(root)
entry.grid(row = 0, column = 1)

Adding a Generate Button

Next, we will add a button to our window that will be used to generate the random number.

button = tk.Button(root, text = "Generate", command = generate_number)
button.grid(row = 1, column = 0)

Defining the generate_number Function

The generate_number function will be called when the user clicks the “Generate” button. This function will use the random module to generate a random number within the range specified by the user.

import random
def generate_number():
    range = int(entry.get())
    number = random.randint(1, range)
    label.config(text = "Random number: " + str(number))

Adding a Reset button

To make the application more user-friendly, we can add a “Reset” button which will allow the user to clear the input in the entry widget and start over.

reset_button = tk.Button(root, text = "Reset", command = reset)
reset_button.grid(row = 1, column = 1)

Defining the reset function

The reset function will be called when the user clicks the “Reset” button. This function will clear the input in the entry widget and change the label back to the original message.

def reset():
    entry.delete(0, tk.END)
    label.config(text = "Enter the range:")

Improving the layout

To improve the layout of the application, we can make use of the columnspan and padx options in the grid method. We can also adjust the width of the entry widget to better match the size of the label and buttons.

label.grid(row = 0, column = 0, padx = 10)
entry.grid(row = 0, column = 1, padx = 10, columnspan = 2)
entry.config(width = 20)
button.grid(row = 1, column = 0, padx = 10)
reset_button.grid(row = 1, column = 1, padx = 10)

With these additional steps, you will have an application with a more polished look and improved functionality.

See also  New : minion hop misi melompati rintangan 5 kali

Full Code Application

import tkinter as tk
import random

def generate_number():
    range = int(entry.get())
    number = random.randint(1, range)
    label.config(text = "Random number: " + str(number))

def reset():
    entry.delete(0, tk.END)
    label.config(text = "Enter the range:")

root = tk.Tk()
root.title("Random Number Generator")

label = tk.Label(root, text = "Enter the range:")
label.grid(row = 0, column = 0, padx = 10)

entry = tk.Entry(root)
entry.grid(row = 0, column = 1, padx = 10, columnspan = 2)
entry.config(width = 20)

button = tk.Button(root, text = "Generate", command = generate_number)
button.grid(row = 1, column = 0, padx = 10)

reset_button = tk.Button(root, text = "Reset", command = reset)
reset_button.grid(row = 1, column = 1, padx = 10)

root.mainloop()

Running the Application

Finally, we will run the main loop of the Tkinter application, which will display the window and handle events.

root.mainloop()

Output Application

Random Number Generator using Tkinter: A Step-by-Step Guide with Code
Random Number Generator using Tkinter: A Step-by-Step Guide with Code

With these steps, you will have a working Random Number Generator using Tkinter.

Note: This is a basic example to illustrate the process of creating a GUI application using Tkinter. In a real-world application, you would need to consider additional factors such as error handling and user input validation.

Print Friendly, PDF & Email

Author

Leave a Reply