1. Basic Window
A simple Tkinter program that creates a basic window with a title.
from tkinter import Tk root = Tk() root.title("Basic Window") root.geometry("300x200") root.mainloop()
2. Hello World
A Tkinter program that displays a label with "Hello, World!".
from tkinter import Tk, Label root = Tk() root.title("Hello World") label = Label(root, text="Hello, World!") label.pack(pady=20) root.mainloop()
3. Button Click
A Tkinter program with a button that changes the label text when clicked.
from tkinter import Tk, Label, Button def on_click(): label.config(text="Button Clicked!") root = Tk() root.title("Button Click") label = Label(root, text="Hello!") label.pack(pady=20) button = Button(root, text="Click Me", command=on_click) button.pack(pady=10) root.mainloop()
4. Simple Calculator
A basic calculator that performs addition of two numbers.
from tkinter import Tk, Entry, Button, Label def add_numbers(): result = int(entry1.get()) + int(entry2.get()) label_result.config(text="Result: " + str(result)) root = Tk() root.title("Simple Calculator") entry1 = Entry(root) entry1.pack(pady=5) entry2 = Entry(root) entry2.pack(pady=5) button = Button(root, text="Add", command=add_numbers) button.pack(pady=5) label_result = Label(root, text="Result: ") label_result.pack(pady=5) root.mainloop()
5. Drawing on Canvas
A program that allows the user to draw lines on a canvas using the mouse.
from tkinter import Tk, Canvas def paint(event): x1, y1 = (event.x - 1), (event.y - 1) x2, y2 = (event.x + 1), (event.y + 1) canvas.create_oval(x1, y1, x2, y2, fill="black") root = Tk() root.title("Drawing on Canvas") canvas = Canvas(root, width=400, height=400, bg="white") canvas.pack() canvas.bind("<B1-Motion>", paint) root.mainloop()
Comments
Post a Comment