Im making a prescription management app i have 2 classes Presman which i include the gui making methods the Newpreswin class where i call functions from presman to create the data entry gui one of them is def create_treeview: where im using as a listbox to insert rows in a database
class NewpresWin(tk.Toplevel): def init(self, parent, frames, entryfields, labels,calendars,functions=None, closing_event=None): super().init(parent) self.parent=parent self.functions=functions if functions else Functions(self) self.presman = Presman(self)
#... other methods and values self.presman.create_treeview(display_data=False) #i call the method from presman and clear he values #... other methods
class Presman:
# Initializes main window
def init(self, master):
self.master = master
self.functions=Functions(self)
#... other values and methods self.create_treeview() #this method creates treeview in the presman class #other methods
because the code was getting unmanageble with so many rows i split my code the one that already provided in gui.py and the other called functions.py in functions.py i have a method which adds the rows in the treeview before i move them in the database when i called def add_med this method is used to insert the rows in the treeview which is displayed in the NewpesWin window but when i call the method instead of adding the data in the treeview in the newpreswin the method adds them in the treeview in the presman window before i split the code everything was working fine
this is the method i am describing it is in the file functions.py
class Functions: def init(self, Presman): self.presman = Presman
Adds row with medicine in newpreswin treeview
def add_med(self):
# Define a function to handle opening the Presscan file when a row is double-clicked on Treeview
def on_select(event):
item = self.newpreswin.treeview.selection()
if item:
# Get the value of the Presscan column for the selected row
presscan_value = self.newpreswin.treeview.item(item, 'values')[10]
if presscan_value:
# Open the file with webbrowser
webbrowser.open(presscan_value)
else:
messagebox.showerror('Error', 'No file associated with this row.', parent=self.presman.frames['center_frame'])
# Read the values of the entry fields and get the corresponding directories
phaident = self.presman.entryfields['phaident'].get().strip()
medicent = self.presman.entryfields['medicent'].get().strip()
barcode = self.presman.entryfields['barcdent'].get().strip()
uppic = self.presman.labels['uploadlabel'].cget('text')
# Check if any of the fields are empty
if not barcode:
messagebox.showerror('Error', 'Barcode field is empty.', parent=self.presman.frames['center_frame'])
self.presman.entryfields['barcdent'].focus_set()
return
if not phaident:
messagebox.showerror('Error', 'Pharmacy ID field is empty.', parent=self.presman.frames['center_frame'])
self.presman.entryfields['phaident'].focus_set()
return
if not medicent:
messagebox.showerror('Error', 'Medicine field is empty.', parent=self.presman.frames['center_frame'])
self.presman.entryfields['medicent'].focus_set()
return
if not uppic:
messagebox.showerror('Error', 'Please upload the prescription.', parent=self.presman.frames['center_frame'])
return
# Look for the pharmacy directory
pha_dir = os.path.join('c:', os.sep, 'presman', 'Data', phaident)
if not os.path.isdir(pha_dir):
messagebox.showerror(f"Pharmacy directory {pha_dir} not found", parent=self.presman.frames['center_frame'])
# Create the medicine directory if it doesn't exist
med_dir = os.path.join(pha_dir, medicent)
if not os.path.isdir(med_dir):
os.mkdir(med_dir)
# Check if the file exists in the Temp directory
temp_dir = os.path.join('c:', os.sep, 'presman', 'Data', 'Temp')
file_path = os.path.join(temp_dir, f"{barcode}.jpg")
if not os.path.isfile(file_path):
messagebox.showerror(f"File {barcode}.jpg not found in {temp_dir}", parent=self.presman.frames['center_frame'])
# Copy the file to the medicine directory
med_file = os.path.join(med_dir, f"{barcode}.jpg")
shutil.copyfile(file_path, med_file)
# Fetch the last inserted Cntr value from the database and increment the treeview index by 1
self.cursor.execute("SELECT MAX(Cntr) FROM data")
last_Cntr = self.cursor.fetchone()[0]
if last_Cntr is None:
Cntr = 1
else:
Cntr = last_Cntr + 1
if hasattr(self, 'current_index'):
self.current_index + 1
else:
self.current_index = 0
Cntr += self.current_index
# Set the values for the new row
Barcode = barcode
Pharmacy = self.presman.labels['phaidreslabel'].cget("text")
Medicine = medicent
Qnt = self.presman.entryfields['quantent'].get()
Pres = self.presman.calendars['presdate'].get_date()
Exp = self.presman.calendars['expdate'].get_date()
Comment = self.presman.entryfields['ucomment'].get()
User = open('userlog.txt', 'r').read().strip()
Timestamp = datetime.now().strftime('%d-%m-%Y %H:%M:%S')
Presscan = med_file
# Update the Presscan column in the Treeview widget
self.newpreswin.treeview.set(self.newpreswin.treeview.selection(), 'Presscan', med_file)
# Insert the new row into the Treeview widget
self.newpreswin.treeview.insert('', 'end', values=(Cntr, Barcode, Pharmacy, Medicine, Qnt, Pres, Exp, Comment, User, Timestamp, Presscan), tags=('clickable',))
# Clear the medicent and quantent fields
self.presman.entryfields['medicent'].delete(0, 'end')
self.presman.entryfields['quantent'].delete(0, 'end')
self.newpreswin.treeview.bind('<Double-Button-1>', on_select)
the method was working just fine until i split the code i have created all the necessary instances and other methods are working as expected except this one