0% found this document useful (0 votes)
2 views4 pages

Python GUI Syntax Guide

This document is a comprehensive guide to creating graphical user interfaces (GUIs) using Tkinter in Python. It covers essential components such as windows, labels, buttons, entry widgets, and various layout managers, along with examples of how to implement them. Additionally, it includes information on event handling, styling, and packaging the application as an executable file.

Uploaded by

amantlesephiri71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Python GUI Syntax Guide

This document is a comprehensive guide to creating graphical user interfaces (GUIs) using Tkinter in Python. It covers essential components such as windows, labels, buttons, entry widgets, and various layout managers, along with examples of how to implement them. Additionally, it includes information on event handling, styling, and packaging the application as an executable file.

Uploaded by

amantlesephiri71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Complete Python GUI Syntax Guide (Tkinter)

1. Importing Tkinter
import tkinter as tk # Standard way
from tkinter import * # Optional, imports everything

2. Creating the Main Window


window = tk.Tk()
window.title("My App")
window.geometry("400x300")
window.resizable(False, False)

3. Labels (Display Text)


label = tk.Label(window, text="Hello, World!", font=("Arial", 14), fg="blue",
bg="white")
label.pack()

4. Entry Widget (Text Input)


entry = tk.Entry(window, width=30)
entry.pack()
user_input = entry.get()

5. Button (Clickable Action)


def on_click():
print("Button Clicked!")

button = tk.Button(window, text="Click Me", command=on_click, bg="green", fg="white")


button.pack()

6. MessageBox (Alerts)
from tkinter import messagebox

messagebox.showinfo("Title", "This is an info box")


messagebox.showwarning("Warning", "This is a warning box")
messagebox.showerror("Error", "This is an error box")

7. Text Widget (Multiline Text)


text_box = tk.Text(window, height=5, width=30)
text_box.pack()
content = text_box.get("1.0", "end-1c")

8. Checkbuttons (Checkboxes)
var = tk.IntVar()
check = tk.Checkbutton(window, text="Accept Terms", variable=var)
check.pack()
value = var.get()

9. Radiobuttons (Single-Choice Option)


var = tk.StringVar()
radio1 = tk.Radiobutton(window, text="Option 1", variable=var, value="1")
Complete Python GUI Syntax Guide (Tkinter)

radio2 = tk.Radiobutton(window, text="Option 2", variable=var, value="2")


radio1.pack()
radio2.pack()
choice = var.get()

10. Listbox (Selectable List)


listbox = tk.Listbox(window)
listbox.insert(1, "Item 1")
listbox.insert(2, "Item 2")
listbox.pack()
selected = listbox.get(listbox.curselection())

11. Combobox (Dropdown List)


from tkinter import ttk
combo = ttk.Combobox(window, values=["Apple", "Banana", "Cherry"])
combo.current(0)
combo.pack()
selected = combo.get()

12. Spinbox (Number Selector)


spin = tk.Spinbox(window, from_=0, to=10)
spin.pack()
value = spin.get()

13. Frames (Group Widgets)


frame = tk.Frame(window, bg="lightgrey", padx=10, pady=10)
frame.pack()
label = tk.Label(frame, text="Inside Frame")
label.pack()

14. Menus
menu = tk.Menu(window)
window.config(menu=menu)
file_menu = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Open")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=window.quit)

15. Canvas (Drawing Area)


canvas = tk.Canvas(window, width=200, height=100, bg="white")
canvas.pack()
canvas.create_rectangle(10, 10, 100, 50, fill="blue")
canvas.create_line(0, 0, 200, 100)
canvas.create_text(100, 50, text="Hello", fill="red")

16. Using .grid() Layout Manager


label = tk.Label(window, text="Name:")
label.grid(row=0, column=0)
Complete Python GUI Syntax Guide (Tkinter)

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

17. Using .place() Layout Manager


label = tk.Label(window, text="Welcome")
label.place(x=50, y=100)

18. Styling with ttk


from tkinter import ttk
style = ttk.Style()
style.configure("TButton", font=("Arial", 12), padding=10)
button = ttk.Button(window, text="Styled Button")
button.pack()

19. Events and Bindings


def on_key_press(event):
print(f"Key pressed: {event.char}")

window.bind("<Key>", on_key_press)

def on_mouse_click(event):
print(f"Clicked at {event.x}, {event.y}")

window.bind("<Button-1>", on_mouse_click)

20. Running the GUI (Main Loop)


window.mainloop()

21. Full Working GUI Example


import tkinter as tk
from tkinter import messagebox

def greet():
name = entry.get()
if name:
messagebox.showinfo("Greeting", f"Hello, {name}!")
else:
messagebox.showwarning("Input Error", "Please enter your name.")

window = tk.Tk()
window.title("Greeting App")
window.geometry("300x150")

label = tk.Label(window, text="Enter your name:")


label.pack()

entry = tk.Entry(window)
entry.pack()

button = tk.Button(window, text="Greet", command=greet)


Complete Python GUI Syntax Guide (Tkinter)

button.pack()

window.mainloop()

22. Packaging as .exe


pip install pyinstaller
pyinstaller --onefile your_file.py

You might also like