Photo Sideshow

import tkinter as tk

from tkinter import *

from PIL import Image

from PIL import ImageTk

root=tk.Tk()

root.geometry("200x200")

img=ImageTk.PhotoImage(Image.open("photo1.png"))

img2=ImageTk.PhotoImage(Image.open("photo2.png"))

img3=ImageTk.PhotoImage(Image.open("photo3.png"))

l=Label()

l.pack()

x = 1

def move():

    global x

    if x == 4:

        x = 1

    if x == 1:

        l.config(image=img)

    elif x == 2:

        l.config(image=img2)

    elif x == 3:

        l.config(image=img3)

    x = x+1

    root.after(2000, move)

move()

root.mainloop()


memory game

from random import *

from turtle import *

screen = Screen()

screen.bgcolor("yellow")

def Square(x, y):

    up()

    goto(x, y)

    down()

    color('white', 'red')

    begin_fill()

    for count in range(4):

        forward(50)

        left(90)

    end_fill()

def numbers(x, y):

    return int((x + 200) // 50 + ((y + 200) // 50) * 8)

def Coordinates(count):

    return (count % 8) * 50 - 200, (count // 8) * 50 - 200

def click(x, y):

    spot = numbers(x, y)

    mark = state['mark']

    if mark is None or mark == spot or tiles[mark] != tiles[spot]:

        state['mark'] = spot

    else:

        hide[spot] = False

        hide[mark] = False

        state['mark'] = None

def draw():

    clear()

    goto(0, 0)

    stamp()

    for count in range(64):

        if hide[count]:

            x, y = Coordinates(count)

            Square(x, y)

    mark = state['mark']

    if mark is not None and hide[mark]:

        x, y = Coordinates(mark)

        up()

        goto(x + 2, y)

        color('white')

        write(tiles[mark], font=('Arial', 30, 'normal'))

    update()

    ontimer(draw, 10)

tiles = list(range(32)) * 2

state = {'mark': None}

hide = [True] * 64

shuffle(tiles)

tracer(False)

onscreenclick(click)

draw()

done()


Background remover app

import tkinter as tk

from rembg import remove

from PIL import Image

from tkinter import filedialog

from PIL import Image

import os

from tkinter import messagebox

top = tk.Tk()

top.geometry("400x400")

top.title('Background Remover')

filename = ''

def upload_file():

    global filename

    f_types = [('Jpg Files', '*.jpg')]

    filename = filedialog.askopenfilename(filetypes=f_types)

    if len(filename) > 0:

        b1.config(state='disabled')

def Convert(image_name):

    current_working_directory = os.getcwd()

    input_path = filename

    output_path = f'{current_working_directory}\{image_name}.png'

    image_input = Image.open(input_path)

    output = remove(image_input)

    output.save(output_path)

    messagebox.showinfo('Success', 'Image background successfully removed')

    top.destroy()

my_font1 = ('times', 18, 'bold')

l1 = tk.Label(top, text='Background Removal App', width=30, font=my_font1)

l1.grid(row=1, column=1)

b1 = tk.Button(top, text='Select here', height=2, font=('Arial', 20), bg='green', fg='white', command=lambda: upload_file())

b1.grid(row=2, column=1, pady=20)

image_name = tk.StringVar(top)

image_name.set('enter file name')

e1 = tk.Entry(top, textvariable=image_name)

e1.grid(row=3, column=1, pady=20)

b2 = tk.Button(top, text='Convert now', height=2, font=('Arial', 20), bg='green', fg='white', command=lambda: Convert(image_name.get()))

b2.grid(row=4, column=1, pady=20)

top.mainloop()

File Explorer

from tkinter import *

from tkinter import filedialog

def browseFiles():

    filename = filedialog.askopenfilename(initialdir = "/",

                                        title = "Select a File",

                                        filetypes = (("Text files",

                                                        "*.txt*"),

                                                    ("all files",

                                                        "*.*")))

    label_file_explorer.configure(text="File Opened: "+filename)                                                                                              

window = Tk()

window.title('File Explorer')

window.geometry("500x500")

window.config(background = "white")

label_file_explorer = Label(window,

                            text = "File Explorer using Tkinter",

                            width = 100, height = 4,

                            fg = "blue")

button_explore = Button(window,

                        text = "Browse Files",

                        command = browseFiles)

button_exit = Button(window,

                    text = "Exit",

                    command = exit)

label_file_explorer.grid(column = 1, row = 1)

button_explore.grid(column = 1, row = 2)

button_exit.grid(column = 1,row = 3)

window.mainloop()


Screenshot using pyscreenshot

The simplest way of taking a screenshot using pyscreenshot module

Install the package:

               pip install pyscreenshot 


import pyscreenshot

image = pyscreenshot.grab()

image.show()

image.save("screenshot.png")