Graphical User Interface (GUI) in Python (1)
Graphical User Interface (GUI) in Python (1)
A Graphical User Interface (GUI) is a user-friendly interface that allows interaction with
software applications using graphical elements such as windows, buttons, menus, and icons
instead of command-line interfaces.
Python offers several GUI frameworks that help in building interactive applications. Some of the
most popular ones are:
● Provides various widgets like buttons, labels, text fields, frames, and canvas.
Example:
import tkinter as tk
root = tk.Tk()
label.pack()
root.mainloop()
●
Example:
from PyQt5.QtWidgets import QApplication, QLabel
import sys
app = QApplication(sys.argv)
label.show()
sys.exit(app.exec_())
●
3. Kivy
Example:
from kivy.app import App
class MyApp(App):
def build(self):
MyApp().run()
●
5. wxPython
6. PyGTK
Example:
import tkinter as tk
root = tk.Tk()
frame.pack()
root.mainloop()
Canvas
● A drawing area that allows users to create custom graphics, shapes, images, and
animations.
Example:
import tkinter as tk
root = tk.Tk()
canvas.pack()
canvas.create_rectangle(50, 50, 200, 150, fill="blue")
root.mainloop()
Frame
● A container widget used to group other widgets together.
Example:
import tkinter as tk
root = tk.Tk()
frame.pack()
root.mainloop()
Message Displays multi-line text like a label but supports word wrapping.
1. Button
import tkinter as tk
def on_click():
print("Button Clicked!")
root = tk.Tk()
btn.pack()
root.mainloop()
2. Label
import tkinter as tk
root = tk.Tk()
label.pack()
root.mainloop()
import tkinter as tk
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
root.mainloop()
import tkinter as tk
root = tk.Tk()
text_box.pack()
root.mainloop()
5. Scrollbar
import tkinter as tk
root = tk.Tk()
text_box.pack(side="left")
scroll.pack(side="right", fill="y")
text_box.config(yscrollcommand=scroll.set)
root.mainloop()
6. Checkbutton
import tkinter as tk
root = tk.Tk()
var1 = tk.IntVar()
chk.pack()
root.mainloop()
7. Radiobutton
import tkinter as tk
root = tk.Tk()
var = tk.IntVar()
rb1.pack()
rb2.pack()
root.mainloop()
8. Spinbox
import tkinter as tk
root = tk.Tk()
spin = tk.Spinbox(root, from_=0, to=10)
spin.pack()
root.mainloop()
9. Message
import tkinter as tk
root = tk.Tk()
msg = tk.Message(root, text="This is a message widget that supports multiple lines of text.")
msg.pack()
root.mainloop()
Conclusion
Python provides several powerful GUI frameworks such as Tkinter, PyQt, and Kivy to create
interactive applications. Understanding containers, widgets, and event-driven programming
helps in building professional and user-friendly applications.