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.
Show Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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()
|
Show Code
1 2 3 4 5 6 7 | 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
1 2 3 4 5 6 7 | 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)?
Show Code
1 2 3 4 | file = open ( "File3.txt" )
for x in file :
print (x.splitlines())
file .close()
|
Show code
1 2 3 4 5 6 7 8 9 10 11 | 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?
Show Code
1 2 3 4 5 6 7 8 9 10 11 12 | 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?
Show Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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
1 2 3 4 5 6 7 8 | 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
1 2 3 4 5 | 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?
Show Code
1 2 3 4 5 6 7 8 9 10 | 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?
Show Code
1 2 3 4 5 6 7 8 9 10 | 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?
Show Code
1 2 3 4 5 6 7 8 | 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.