Creating a tkinter GUI layout using frames and grid

TkinterPythonGUI-Programming

Tkinter is a well-known Python library for creating GUI-based applications. You can create a fully featured application with the help of widgets, functions, and modules that are already present in Tkinter library.

Sometimes, selecting the right GUI of the application becomes a daunting task for many of us. Tkinter comes with a set of inbuilt functions and extensions to create good-looking GUIs.

Generally, the Frame widget in Tkinter manages all the widgets in an application as a container. It inherits all the properties that the main window can contain. To design the layout of the widgets, we can use any of the geometry managers. Geometry Manager helps to create the layout of the widget and place them in a certain order. The Grid geometry manager places all the widgets in the form of X and Y coordinate system. We can provide the row and column property to place the widget anywhere in the application.

Example

In this example, we will create the GUI of a Registration Form.

# Import the Required libraries
from tkinter import *

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the window
win.geometry("700x350")

# Add a Frame widget
frame = Frame(win)

# Define a function to get the data and display a message
def on_key_up():
   name = fname
   frame.pack_forget()
   win.configure(bg="green4")
   Label(win, text="Hey " + fname.get() + " Welcome to TutorialsPoint",
      font=('Segoe UI', 18, 'bold'),
      background="white").pack(pady=30)
# Create a Label widget
   Label(frame, text="Registration Form",
   font=('Helvetica 16 bold'),
background="green3").grid(row=5, column=0, pady=30)
frame.pack()

# Add Field for First Value
Label(frame, text="First Name").grid(row=7, column=0, padx=5)
fname = Entry(frame)
fname.grid(row=10, column=0, padx=10)

# Add Field for Second Value
Label(frame, text="Family name").grid(row=12, column=0, padx=5)
sname = Entry(frame)
sname.grid(row=15, column=0, padx=10)

# Add Field for Email Address
Label(frame, text="Email address").grid(row=17, column=0, padx=5)
email = Entry(frame)
email.grid(row=20, column=0, padx=10)

# Add another field for accepting password value
Label(frame, text="Enter a Password").grid(row=22, column=0, padx=5)
password = Entry(frame, show="*")
password.grid(row=25, column=0, padx=10)

# Create a button
Button(frame, text="Register", command=on_key_up).grid(row=15,
column=1, padx=5)

win.mainloop()

Output

Running the above code will display a registration form template and a button to register the information.

raja
Published on 07-Jun-2021 11:19:55
Advertisements