Snippets / python
Rotate Screen Using Python
pythonRotating screen orientation programmatically using Python 6 line of code
import rotatescreen
import time
screen= rotatescreen.get_primary_display()
for i in range(13):
time.sleep(1)
screen.rotate_to(i*90 % 360)
Sep 17, 2024
Show code
Web Scraping Using Python
pythonWeb scraping tool built with Python, designed to extract and process data from websites.
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = "https://apksoftwared.com/apps/"
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'}
# You can use proxies for random IP. So that your IP can't be blocked by any site.
req = requests.get(url, headers=headers)
soup = BeautifulSoup(req.text, "html.parser")
data = {"Image": [], "Title": [], "Version": [], "Links": []}
postTitles = soup.select("span.title")
bavs = soup.find_all(class_="bav1")
for img in bavs:
# When you print the img tags in html you can see that the url of the images are written like this=> data-src="image_url"
# So, if you write ['src'] it will return base64 format. That's why, use ['data-src'] it will return the main url of the img.
data["Image"].append(img.find("a").find(class_="bloque-imagen").find("img")["data-src"])
for postTitle in postTitles:
data["Title"].append(postTitle.get_text())
for link in bavs:
data["Links"].append(link.find("a").get("href"))
for version in bavs:
data["Version"].append(version.find("a").find(class_="version").string)
df = pd.DataFrame.from_dict(data)
df.to_csv("data.csv", index=False)
Sep 17, 2024
Show code
data:image/png;base64 Decode
pythonPython code for decoding Base64-encoded PNG images into viewable formats
# Decode data:image/png;base64 image
from PIL import Image
import base64
from io import BytesIO
def decodeImage(base64Image_data):
base64Image_data = base64Image_data.split(b',')[1]
decodeImage = base64.b64decode(base64Image_data)
result = BytesIO(decodeImage)
image = Image.open(result)
return image
base64Data = b'data:image/png;base64...'
image = decodeImage(base64Data)
image.show()
Sep 17, 2024
Show code
Image Background Remover GUI
pythonUser-friendly GUI application for removing image backgrounds, developed with Python and Tkinter.
from tkinter import *
from rembg import remove
from PIL import Image
from tkinter import filedialog
root = Tk()
root.geometry("500x200")
root.maxsize(500, 200)
root.minsize(500, 200)
root.title("RemoverPy")
head_label = Label(root, text="Remove image background", bg="#222831", fg="white", pady=10, font="Roboto 16 bold").pack(side=TOP, fill=X)
def removeImageBg():
filename = filedialog.askopenfilename(initialdir = "/", title = "Select a PNG file 😃", filetypes = (("Png files", "*.png*"),))
try:
if filename == "":
caption.configure(text="Empty image cannot be processed :-(", fg="red")
else:
openImg = Image.open(filename)
removeBg = remove(openImg)
removeBg.save("removedbg.png")
caption.configure(text="Successful :)", fg="#11934A")
except Exception as e:
caption.configure(text="Something went wrong", fg="red")
selectImgBtn = Button(text="Choose image :)", padx=30, pady=10, bg="#76ABAE", fg="#31363F", cursor="heart", font="Roboto 10 bold", command=removeImageBg).pack(side=TOP, anchor=CENTER, pady=20)
caption = Label(root, text="", font="Roboto 10 bold")
caption.pack(side=TOP, anchor=CENTER)
root.mainloop()
Sep 17, 2024
Show code
Py Calculator
pythonPython Tkinter-based calculator with a simple and intuitive interface for performing basic arithmetic operations easily.
from tkinter import *
def click(event):
global screenValue
btn_text = event.widget.cget("text")
if btn_text == "=":
if screenValue.get() != "":
result = eval(screenValue.get())
screenValue.set(result)
screen.update()
else:
pass
elif btn_text == "AC":
screenValue.set("")
else:
screenValue.set(screenValue.get() + btn_text)
if __name__ == "__main__":
root = Tk()
root.geometry("300x630")
root.maxsize(300, 630)
root.minsize(300, 630)
frame_bg_color = "#eae9e9"
main_frame = Frame(root, bg=frame_bg_color)
main_frame.pack(side=TOP, anchor=CENTER, fill=BOTH, expand=1)
header_frame = Frame(main_frame, bg=frame_bg_color)
header_frame.pack(side=TOP, anchor=CENTER, fill=X)
Label(header_frame,
text="Py Calculator",
font=("Giddyup Std", "15", "bold"),
bg=frame_bg_color,
fg="#135D66").pack(side=LEFT, padx=2, pady=10)
screen_frame = Frame(main_frame, bg=frame_bg_color)
screen_frame.pack(side=TOP, anchor=CENTER, fill=X)
screenValue = StringVar()
screenValue.set("")
screen = Entry(screen_frame,
textvariable=screenValue,
font=("Giddyup Std", "25", "bold"),
fg="black",
borderwidth=5)
screen.pack(side=TOP, anchor=CENTER, ipady=40, padx=5)
screen.config(state=DISABLED)
calBtns_grid_frame = Frame(main_frame, bg=frame_bg_color)
calBtns_grid_frame.pack(side=TOP, anchor=CENTER, pady=20)
btn_padX = 10
btn_padY = 5
font_btn = ("Giddyup Std", "25", "bold")
grid_padX = 3
grid_padY = 3
btn_fg_color = "#003C43"
btn_bg_color = "#dadada"
btn = Button(calBtns_grid_frame,
text="AC",
padx=69,
pady=btn_padY,
font=font_btn,
fg="white",
bg="#de2b2b")
btn.grid(row=0, column=0, padx=grid_padX, pady=grid_padY, columnspan=3)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="/",
padx=15,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=0, column=3, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="7",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=1, column=0, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="8",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=1, column=1, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="9",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=1, column=2, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="*",
padx=13,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=1, column=3, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="4",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=2, column=0, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="5",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=2, column=1, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="6",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=2, column=2, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="-",
padx=14,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=2, column=3, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="1",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=3, column=0, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="2",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=3, column=1, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="3",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=3, column=2, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="+",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=3, column=3, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="0",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=4, column=0, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="%",
padx=6,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=4, column=1, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text=".",
padx=14,
pady=btn_padY,
font=font_btn,
fg=btn_fg_color,
bg=btn_bg_color)
btn.grid(row=4, column=2, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
btn = Button(calBtns_grid_frame,
text="=",
padx=btn_padX,
pady=btn_padY,
font=font_btn,
fg="black",
bg="#08da8d")
btn.grid(row=4, column=3, padx=grid_padX, pady=grid_padY)
btn.bind('<Button-1>', click)
root.mainloop()
Sep 17, 2024
Show code
Password Generator Tkinter Application
pythonSecure password generator application built with Python and Tkinter, offering customizable password options with a user-friendly interface.
from tkinter import *
from threading import *
import string
import random
import tkinter.messagebox as tmsg
import pandas as pd
import os
def thread_saving():
thr = Thread(target=saveToCsv)
thr.start()
def saveToCsv():
data = {"Name": [], "Password": []}
data["Name"].append(nameVal.get())
data["Password"].append(passDisplay.get())
file_exist = os.path.isfile("password.csv")
if nameVal.get() != "":
if passDisplay.get() != "":
try:
dataFrame = pd.DataFrame.from_dict(data)
if file_exist:
dataFrame.to_csv("password.csv",
mode="a",
index=False,
header=False)
else:
dataFrame.to_csv("password.csv",
mode="a",
index=False,
header=True)
systemStatus.configure(text="Password has been saved",
fg="green")
except:
systemStatus.configure(text="Failed to save password",
fg="red")
tmsg.showerror(
"Error",
"Something went wrong! Please close the password.csv file then try again!"
)
else:
tmsg.showerror("Error", "Empty password cannot save")
else:
tmsg.showerror("Error", "Name your password")
def click(event):
type_pass_lenEntry.config(state=DISABLED)
lenEntryVal.set("")
def thread_generatePass():
thr = Thread(target=generatePass)
thr.start()
def generatePass():
lowCase = string.ascii_lowercase
uppCase = string.ascii_uppercase
digits = string.digits
punc = string.punctuation
ls = []
ls.extend(list(lowCase))
ls.extend(list(uppCase))
ls.extend(list(digits))
ls.extend(list(punc))
random.shuffle(ls)
try:
if radioVal.get() != 0:
finalRes = "".join(ls[0:radioVal.get()])
elif lenEntryVal.get() != "":
finalRes = "".join(ls[0:lenEntryVal.get()])
else:
tmsg.showerror("Error", "Select password length")
passDisplay.set(finalRes)
genPassBtn.configure(text="Regenerate")
systemStatus.configure(text="Password generated", fg="green")
except:
tmsg.showerror("Error", "Please enter integer value")
lenEntryVal.set("")
def clearAll():
[
widget.delete(0, END) for widget in genPass_frame.winfo_children()
if isinstance(widget, Entry)
]
radioVal.set(0)
type_pass_lenEntry.config(state=NORMAL)
lenEntryVal.set("")
genPassBtn.configure(text="Generate")
systemStatus.configure(text="")
if __name__ == "__main__":
root = Tk()
root.geometry("700x500")
root.minsize(700, 500)
root.maxsize(700, 500)
main_frame = Frame(root)
main_frame.pack(side=TOP, anchor=CENTER, fill=X)
header_frame = Frame(main_frame)
header_frame.pack(side=TOP, anchor=CENTER, fill=X)
Label(header_frame, text="Password Generator",
font="Terminal 20 bold").pack(side=TOP, anchor=CENTER, pady=40)
genPass_frame = Frame(main_frame)
genPass_frame.pack(side=TOP, anchor=CENTER)
radioVal = IntVar()
radioVal.set(0)
nameVal = StringVar()
lenEntryVal = IntVar()
lenEntryVal.set("")
passDisplay = StringVar()
Label(genPass_frame,
text="Name your password",
font=("Consolas", "15", "bold")).grid(row=0,
column=0,
sticky=W,
pady=10,
padx=10)
Label(genPass_frame, text=":",
font=("Consolas", "15", "bold")).grid(row=0, column=1, sticky=W)
nameVal = Entry(genPass_frame, width=30, font=("Consolas", "15"))
nameVal.grid(row=0, column=2, ipady=5)
Label(genPass_frame,
text="Select your password length",
font=("Consolas", "15", "bold")).grid(row=1,
column=0,
pady=10,
sticky=W,
padx=10)
Label(genPass_frame, text=":",
font=("Consolas", "15", "bold")).grid(row=1, column=1, sticky=W)
radioBtn_frame = Frame(genPass_frame)
radioBtn_frame.grid(row=1, column=2)
rabtn = Radiobutton(radioBtn_frame,
text="4",
value=4,
variable=radioVal,
font=("Consolas", "15"),
bg="#F3D0D7",
indicatoron=0,
padx=10)
rabtn.grid(row=0, column=0, padx=10)
rabtn.bind("<Button-1>", click)
rabtn = Radiobutton(radioBtn_frame,
text="6",
value=6,
variable=radioVal,
font=("Consolas", "15"),
bg="#F3D0D7",
indicatoron=0,
padx=10)
rabtn.grid(row=0, column=1, padx=10)
rabtn.bind("<Button-1>", click)
rabtn = Radiobutton(radioBtn_frame,
text="8",
value=8,
variable=radioVal,
font=("Consolas", "15"),
bg="#F3D0D7",
indicatoron=0,
padx=10)
rabtn.grid(row=0, column=2, padx=10)
rabtn.bind("<Button-1>", click)
rabtn = Radiobutton(radioBtn_frame,
text="12",
value=12,
variable=radioVal,
font=("Consolas", "15"),
bg="#F3D0D7",
indicatoron=0,
padx=10)
rabtn.grid(row=0, column=3, padx=10)
rabtn.bind("<Button-1>", click)
rabtn = Radiobutton(radioBtn_frame,
text="15",
value=15,
variable=radioVal,
font=("Consolas", "15"),
bg="#F3D0D7",
indicatoron=0,
padx=10)
rabtn.grid(row=0, column=4, padx=10)
rabtn.bind("<Button-1>", click)
Label(genPass_frame,
text="Or, set custom length",
font=("OCR A Std", "15", "bold")).grid(row=2,
column=0,
pady=10,
sticky=W,
padx=10)
Label(genPass_frame, text=":",
font=("OCR A Std", "15", "bold")).grid(row=2, column=1, sticky=W)
type_pass_lenEntry = Entry(genPass_frame,
width=10,
font=("Consolas", "15"),
textvariable=lenEntryVal)
type_pass_lenEntry.grid(row=2, column=2, ipady=5, sticky=W)
Entry(genPass_frame,
textvariable=passDisplay,
width=50,
font=("Consolas", "15")).grid(row=4,
column=0,
columnspan=3,
ipady=10,
pady=30)
systemStatus = Label(genPass_frame,
text="",
font=("Consolas", "13", "bold"))
systemStatus.grid(row=5, column=0, columnspan=3)
download_button = Button(genPass_frame,
text="Save",
padx=20,
font=("Consolas", "10", "bold"),
bg="#8DECB4",
command=thread_saving).grid(row=4,
column=2,
sticky=E)
genPassBtn = Button(genPass_frame,
text="Generate",
command=thread_generatePass,
padx=20,
pady=5,
font=("Consolas", "15"),
bg="#6DC5D1")
genPassBtn.grid(row=6, column=0, columnspan=3, pady=50)
Button(genPass_frame,
text="Reset",
command=clearAll,
padx=20,
pady=5,
font=("Consolas", "15"),
bg="#FA7070").grid(row=6, column=1, columnspan=3, pady=50)
root.mainloop()
Sep 17, 2024
Show code