Python Problem Solving



Welcome to the Python Problem Solving section. A collection of many different scenarios and problems that may arise during your Coding journey. Follow the individual links to see the main article which contains a detailed step by step break down of the solution.

Q. How to create Backups in Python using Zip Files?

Show Code
from zipfile import ZipFile
import os

extension = input('Input file extension: ')

Zippy = ZipFile("Backup.zip", 'w')

for folder, subfolders, file in os.walk("C:\\Users"):
    for subfolders in subfolders:
        path = folder + subfolders 
    for x in file:
        if x.endswith(extension):
            filepath = folder + "\\" + x
            print(filepath)
            Zippy.write(filepath, compress_type=zipfile.ZIP_DEFLATED)
Zippy.close()

Q. How to iterate through multi dimensional Arrays?

Show Code
import numpy as np
 
arr2 = np.array([[1,2,3],
                [4,5,6]])
 
for x in np.nditer(arr2):
    print(x)

Q. How to take input from the User with a user interface (Tkinter GUI)?

Show Code
from tkinter.simpledialog import askstring

root = tk.Tk()
prompt = askstring("Input", "Input an String")
print(prompt)

root.mainloop()

Q. How to update/change values Dynamically on a GUI (Tkinter)?

Q. How to remove the newline (/n) character while reading from Files?

Show Code
file = open("File3.txt")
for x in file:
    print(x.splitlines()) 
file.close()

Q. How to create a spreadsheet (Tkinter GUI)?

Show code
from tkinter import *
 
root = Tk()
root.geometry('300x300')
 
for x in range(10):
    for y in range(6):
         entry = Entry(root, width = 6)
         entry.grid(row = x, column = y)
 
root.mainloop(

Q. How to create an Undo button (Tkinter GUI)?

Q. How to edit a file?

Q. How to improve Tkinter GUI screen resolution? (Fix for blurriness)

Show Code
import tkinter as tk
import ctypes
 
ctypes.windll.shcore.SetProcessDpiAwareness(1)
 
root = tk.Tk()
root.geometry("200x150")
 
label = tk.Label(root, text = "Hello World")
label.pack(padx = 5, pady = 5)
 
root.mainloop()

Q. How to skip Captcha and bot checks while Web scraping?

Q. How to create a save and load system for a game or application?

Q. How to dynamically update values in a Matplotlib graph?

Q. How to create multiple Tkinter GUI windows?

Show Code
from tkinter import *

def NewWindow():
    window = Toplevel()
    window.geometry('150x150')
    newlabel = Label(window, text = "Settings Window")
    newlabel.pack()


root = Tk()
root.geometry('200x200')

myframe = Frame(root)
myframe.pack()

mybutton = Button(myframe, text = "Settings", command = NewWindow)
mybutton.pack(pady = 10)

root.mainloop()

Q. How to take a password as input from the User (Tkinter)?

Show Code
from tkinter import *

root = Tk()
root.geometry('150x100')
widget = Entry(root, show="*", width=15)
widget.pack()

root.mainloop()

Alternate technique without Tkinter.

Q. How to create an Open File dialog?

Show Code
import tkinter as tk
from tkinter import filedialog 

path = filedialog.askopenfilename(initialdir="/", title="Select file",
                    filetypes=(("txt files", "*.txt"),("all files", "*.*")))

Q. How to check if a specific File paths exists?

Q. How to change the font type and size in Tkinter?

Show Code
import tkinter as tk

root = tk.Tk()
root.option_add('*Font', 'Times 19')
root.geometry("200x150")

label = tk.Label(root, text = "Hello World")
label.pack(padx = 5, pady = 5)

root.mainloop()

Q. Trigger a Function if user tries to close Tkinter Window

Q. How to make a time/date based expiration for my software?

Q. How to disable resizing in a Tkinter Window?

Show Code
import tkinter as tk

root = tk.Tk()
root.geometry("200x150")
 
label = tk.Label(root, text = "Hello World")
label.pack(padx = 5, pady = 5)
 
root.resizable(False, False) 
root.mainloop()

Q. How to make my Tkinter window start up in the center?

Q. How to subtract two dates from each other?

Show Code
import datetime

a = datetime.datetime(2021,3,20,12)
b = datetime.datetime(2020,4,11,8)
c = a - b

print("Days: ",c.days)
print("seconds: ",c.seconds)

This marks the end of the Python problem solving section. Any suggestions or contributions are more than welcome. Any questions can be asked below in the comments section. Help us improve CodersLegacy.

Leave a Reply

Your email address will not be published. Required fields are marked *