I'm new to python, and I'm making simple code to open a window that has two buttons< one for input and the other for output. The input button opens a new window and also has two buttons, the first button is for browsing a pdf file, and the second is for saving the file into a folder called "sanaa" in c. The code is written with chatGPT assistance.
from tkinter import ttk, filedialog
import shutil
import os
def browse_pdf():
file_path = filedialog.askopenfilename(title="Select a PDF file", filetypes=[("PDF files", "*.pdf")])
entry_path.delete(0, tk.END)
entry_path.insert(0, file_path)
def save_to_sanaa():
source_path = entry_path.get()
if not source_path:
output_label.config(text="Please select a PDF file first.")
return
destination_folder = "C:\\sanaa"
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
destination_path = os.path.join(destination_folder, os.path.basename(source_path))
shutil.copy2(source_path, destination_path)
output_label.config(text=f"PDF saved to: {destination_path}")
def open_input_window():
input_window = tk.Toplevel(root)
input_window.title("Input Window")
input_window.geometry("400x300")
label = ttk.Label(input_window, text="Input Window", font=("Arial", 18))
label.pack(pady=20)
#entry_path = ttk.Entry(input_window, width=30)
#entry_path.pack(pady=10)
browse_button = ttk.Button(input_window, text="Browse PDF", command=browse_pdf)
browse_button.pack(pady=10)
save_button = ttk.Button(input_window, text="Save to Sanaa", command=save_to_sanaa)
save_button.pack(pady=10)
close_button = ttk.Button(input_window, text="Close", command=input_window.destroy)
close_button.pack(pady=10)
output_label = ttk.Label(input_window, text="")
output_label.pack(pady=10)
def open_output_window():
output_window = tk.Toplevel(root)
output_window.title("Output Window")
output_window.geometry("400x300")
label = ttk.Label(output_window, text="Welcome to Output Window!", font=("Arial", 18))
label.pack(pady=20)
close_button = ttk.Button(output_window, text="Close", command=output_window.destroy)
close_button.pack(pady=10)
# Main window
root = tk.Tk()
root.title("Simple GUI")
root.geometry("600x400")
style = ttk.Style()
style.theme_use("default") # You can experiment with different themes, like "clam", "vista", "aqua", etc.
style.configure('TButton', font=('Arial', 14), padding=10)
style.configure('TLabel', font=('Arial', 14), padding=10)
style.configure('TEntry', font=('Arial', 12), padding=10)
button_input = ttk.Button(root, text="Open Input Window", command=open_input_window)
button_input.pack(pady=20)
button_output = ttk.Button(root, text="Open Output Window", command=open_output_window)
button_output.pack(pady=20)
root.mainloop()
The below error is what I get whenever I run the code
Traceback (most recent call last):
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\lenovo\PycharmProjects\pythonProject\.venv\Scripts\CompleteCode.py", line 8, in browse_pdf
entry_path.delete(0, tk.END)
NameError: name 'entry_path' is not defined
Any suggestions?
Note that variables created inside function are local variables if they are not declared as global variables, so they cannot be accessed outside the function.
You can pass them to other functions as arguments instead:
def browse_pdf(entry_path):
...
def save_to_sanaa(entry_path, output_label):
...
def open_input_window():
...
entry_path = ttk.Entry(input_window, width=30)
entry_path.pack(pady=10)
browse_button = ttk.Button(input_window, text="Browse PDF",
command=lambda: browse_pdf(entry_path))
browse_button.pack(pady=10)
save_button = ttk.Button(input_window, text="Save to Sanaa",
command=lambda: save_to_sanaa(entry_path, output_label))
save_button.pack(pady=10)
...