No Widget Added

Please add some widget in Offcanvs Sidebar

Shopping cart

shape
shape

Create GUI window in Python

best python classes, python classes online, python classses near me , how to learn python, skills101.in

Let’s create a simple GUI window in Python that displays “Hello, World!” using the tkinter library.

import tkinter as tk

# Create the main window
root = tk.Tk()
root.title("Hello Window")  # Set the title of the window

# Create a label widget with the text "Hello, World!"
label = tk.Label(root, text="Hello, World!", font=('Arial', 24))

# Position the label in the window
label.pack(padx=20, pady=20)

# Run the main event loop
root.mainloop()

Explanation

  1. Importing the Tkinter Library:
import tkinter as tk

This line imports the tkinter library and assigns it the alias tk. This library is used to create graphical user interfaces in Python.

2. Creating the Main Window:

root = tk.Tk()

root is the main window object.

tk.Tk() initializes the main application window.

root.title("Hello Window")

root.title("Hello Window") sets the title of the window to “Hello Window”.

3. Creating a Label Widget:

label = tk.Label(root, text="Hello, World!", font=('Arial', 24))
  • label is a label widget that displays text.
  • tk.Label() creates the label.
  • root is the parent widget (the main window) where the label will be placed.
  • text="Hello, World!" sets the text of the label.
  • font=('Arial', 24) sets the font to Arial with a size of 24.

4. Positioning the Label in the Window:

label.pack(padx=20, pady=20)
  • label.pack() is a layout manager that places the label in the window.
  • padx=20 adds horizontal padding of 20 pixels around the label.
  • pady=20 adds vertical padding of 20 pixels around the label.

5. Running the Main Event Loop:

root.mainloop()
  • root.mainloop() starts the main event loop of the application.
  • This loop waits for events such as button clicks or key presses and responds accordingly.
  • The program will keep running until the window is closed.

Leave A Comment

Your email address will not be published. Required fields are marked *