# Introduction to GUI Programming

## Overview of GUI frameworks (Tkinter, PyQt, Kivy)

GUI (Graphical User Interface) frameworks allow users to interact with electronic devices via graphical elements. They replace text-based commands with user-friendly actions. [The top Python GUI frameworks include Tkinter, PyQt, and Kivy](https://blog.logrocket.com/comparing-top-python-gui-frameworks/)[1](https://blog.logrocket.com/comparing-top-python-gui-frameworks/). [Each of these frameworks has its pros and cons](https://blog.logrocket.com/comparing-top-python-gui-frameworks/)[1](https://blog.logrocket.com/comparing-top-python-gui-frameworks/).

* **Tkinter**: It is the standard built-in GUI library for Python and is the most popular Python GUI framework. [It provides many of the widgets often used in a GUI application, such as labels, buttons, checkboxes, text boxes, and canvas](https://blog.logrocket.com/comparing-top-python-gui-frameworks/)[1](https://blog.logrocket.com/comparing-top-python-gui-frameworks/).
* **PyQt**: It is a Python GUI framework that’s built around the popular QT application framework. [It runs on many platforms, including Windows, macOS, Linux, iOS, and Android](https://blog.logrocket.com/comparing-top-python-gui-frameworks/)[1](https://blog.logrocket.com/comparing-top-python-gui-frameworks/).
* [**Kivy**: It is an open-source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps](https://blog.logrocket.com/comparing-top-python-gui-frameworks/)[1](https://blog.logrocket.com/comparing-top-python-gui-frameworks/).

1. **Youtube video link talked about this topic**: You can watch the video "Tkinter vs. [PyQt: Which Python GUI framework is right for you?"](https://blog.logrocket.com/comparing-top-python-gui-frameworks/)[2](https://www.youtube.com/watch?v=Dby5jRoENlI)[ and "Python Top GUI Frameworks For 2021"](https://blog.logrocket.com/comparing-top-python-gui-frameworks/)[3](https://www.youtube.com/watch?v=pliF6TkDpxk) on YouTube for a detailed discussion on this topic.

### **Example**

* **Tkinter**:

```python
import tkinter as tk

root = tk.Tk()
label = tk.Label(root, text="Hello, Tkinter!")
label.pack()
root.mainloop()
```

* **PyQt**:

```python
from PyQt5.QtWidgets import QApplication, QLabel

app = QApplication([])
label = QLabel('Hello, PyQt!')
label.show()
app.exec_()
```

* **Kivy**:

```python
from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):
    def build(self):
        return Label(text='Hello, Kivy!')

MyApp().run()
```

1. [**5 Exercise on that topic with questions and answer show separately**: For exercises related to these GUI frameworks, you can refer to the Python Tkinter exercises provided by w3resource](https://www.w3resource.com/python-exercises/tkinter/index.php)[4](https://www.w3resource.com/python-exercises/tkinter/index.php).

## Building Simple Graphical Interfaces

Building simple graphical interfaces involves creating user-friendly applications that allow users to interact with the program through graphical elements like buttons, text boxes, sliders, etc. This is typically done using GUI (Graphical User Interface) libraries. [In Python, one such library is PySimpleGUI](https://realpython.com/pysimplegui-python/)[1](https://realpython.com/pysimplegui-python/). [It simplifies the process of creating GUIs and is gaining a lot of interest recently](https://realpython.com/pysimplegui-python/)[1](https://realpython.com/pysimplegui-python/).

1. [**Youtube video link talked about this topic**: You can watch the video "Tkinter Course - Create Graphic User Interfaces in Python Tutorial"](https://www.youtube.com/watch?v=YXPyB4XeYLA)[2](https://www.youtube.com/watch?v=YXPyB4XeYLA)[ and "Modern Graphical User Interfaces in Python"](https://www.youtube.com/watch?v=iM3kjbbKHQU)[3](https://www.youtube.com/watch?v=iM3kjbbKHQU) on YouTube for a detailed discussion on this topic.

### **Example**

Here is a simple example of creating a graphical interface using PySimpleGUI[1](https://realpython.com/pysimplegui-python/):

```python
import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

window = sg.Window("Demo", layout)

while True:
    event, values = window.read()
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
```

## Handling events and user input using Tkinter

In Python, using the Tkinter GUI package, we can handle events such as button clicks, keyboard input, and mouse interactions by binding them to either predefined or user-defined functions[1](https://instructobit.com/tutorial/51/Python-Tkinter-event-handling). [This allows us to run pieces of code when a certain event occurs within a widget](https://instructobit.com/tutorial/51/Python-Tkinter-event-handling)[1](https://instructobit.com/tutorial/51/Python-Tkinter-event-handling).

1. [**Youtube video link talked about this topic**: You can watch the video "Tkinter Course - Create Graphic User Interfaces in Python Tutorial"](https://instructobit.com/tutorial/51/Python-Tkinter-event-handling)[2](https://www.youtube.com/watch?v=YXPyB4XeYLA)[ and "Accepting User Input In Tkinter Form | Python Tkinter GUI Tutorial In Hindi #12"](https://instructobit.com/tutorial/51/Python-Tkinter-event-handling)[3](https://www.youtube.com/watch?v=dvLMe-L5e-g) on YouTube for a detailed discussion on this topic.

### **Example**

```python
from tkinter import Tk, Label

window = Tk()

def mouseClick(event):
    print("Mouse clicked")

label = Label(window, text="Click me")
label.pack()

label.bind("<Button>", mouseClick)

window.mainloop()
```

## Creating Interactive Applications in Tkinter Python

Creating interactive applications in Tkinter involves designing GUIs that respond to user actions. This is achieved by associating Python functions (event handlers) with user interface elements (like buttons) that generate events (like clicks).

1. [**Youtube video link talked about this topic**: You can watch the video "Tkinter Course - Create Graphic User Interfaces in Python Tutorial"](https://realpython.com/python-gui-tkinter/)[2](https://www.youtube.com/watch?v=YXPyB4XeYLA)[ and "Create a GUI app with Tkinter - Step by Step Tutorial"](https://realpython.com/python-gui-tkinter/)[3](https://www.youtube.com/watch?v=itRLRfuL_PQ) on YouTube for a detailed discussion on this topic.

### **Example**

```python
from tkinter import Tk, Button

def on_button_click():
    print("Button clicked!")

window = Tk()
button = Button(window, text="Click me!", command=on_button_click)
button.pack()

window.mainloop()
```
