mpenn/scripts/folder_mangement/CreateFolder.py

92 lines
3.4 KiB
Python

import customtkinter as Ctk
import os
import scripts.get_sys_info as system_code
from ..SaveData import SaveData
FONT = "Berlin Sans FB"
class CreateFolder(Ctk.CTkFrame):
def __init__(self,master, output_path,callback, **kwargs):
super().__init__(master, **kwargs)
system_code.load_json_file()
self.data_saver = SaveData()
self.my_font = Ctk.CTkFont(family=FONT, size=22)
self.output_path = output_path
self.callback = callback
self.error_txt = ""
self.create_folder()
def enable_keybinding(self):
self.master.bind("<Return>", self.on_a_press, add="+")
def disable_keybinding(self):
self.master.unbind("<Return>")
def on_a_press(self, event):
# Check if the frame is visible by querying its manager info
if self.winfo_manager():
self.create_new_data_folder()
def create_folder(self):
self.create_folder_label = Ctk.CTkLabel(self, text="Type name for new Object:", width=100, font=self.my_font)
self.create_folder_entry = Ctk.CTkEntry(self, placeholder_text="New Data Object", width=100, font=self.my_font)
self.create_folder_name_btn = Ctk.CTkButton(self, text="Create", width=100, command=self.create_new_data_folder, font=self.my_font)
self.create_folder_error = Ctk.CTkLabel(self, text=self.error_txt, width=100, font=self.my_font)
self.place_create_folder()
def create_new_data_folder(self):
"""
Create a new folder in the directory specified by self.output_path.
The folder name will be 'data_folder'. If the folder already exists, a number will be appended
to create a unique folder name ('data_folder_1', 'data_folder_2', etc.).
"""
folder_name = self.create_folder_entry.get()
# Construct the full path for the new data folder
full_path = os.path.join(self.output_path, folder_name)
if os.path.exists(full_path):
self.error_txt = "The folder exists already"
self.create_folder_error.configure(text=self.error_txt)
return
try:
os.makedirs(full_path)
self.data_saver.create(full_path)
except OSError:
self.error_txt = "There occured an error creating the Folder"
self.create_folder_error.configure(text=self.error_txt)
self.callback(full_path)
def place_create_folder(self):
self.create_folder_label.place(
relx=0.5,
rely=0.425,
relwidth=0.3,
relheight=0.06,
anchor="center",
) # Position the converter button to the left
self.create_folder_entry.place(
relx=0.3875,
rely=0.5,
relwidth=0.125,
relheight=0.06,
anchor="w",
) # Position the converter button to the left
self.create_folder_name_btn.place(
relx=0.6125,
rely=0.5,
relwidth=0.08,
relheight=0.06,
anchor="e",
) # Adjusted for .place
self.create_folder_error.place(
relx=0.5,
rely=0.575,
relwidth=0.3,
relheight=0.06,
anchor="center",
) # Position the converter button to the left