main.py
GUI kodas ir make_image() method.unknown
python
8 months ago
5.0 kB
0
Indexable
Never
import PySimpleGUI as sg from db_stuff import Database from PIL import Image import io # Function to display input form and add data to the database def add_data_layout(db): layout = [ [sg.Text("Enter Name:"), sg.InputText(key='-NAME-')], [sg.Text("Enter Age:"), sg.InputText(key='-AGE-')], [sg.Button('Add'), sg.Button('Cancel')] ] window = sg.Window('Add Data', layout) while True: event, values = window.read() if event == sg.WINDOW_CLOSED or event == 'Cancel': break elif event == 'Add': name = values['-NAME-'] age = values['-AGE-'] db.create_data(name, age) sg.popup('Data added successfully!') break window.close() # Function to display all entries in the database def view_data_layout(db): data = db.read_data() image_elem = sg.Image(data=make_image("cat1.jpg")) layout = [ [sg.Table(values=data, headings=['ID', 'Name', 'Age'], auto_size_columns=True, display_row_numbers=False, justification='left', key='-TABLE-'), image_elem], [sg.Button('Add Data'), sg.Button('Delete Selected'), sg.Button('Update Selected'), sg.Button('Close')] ] window = sg.Window('View Data', layout) while True: event, values = window.read() if event == sg.WINDOW_CLOSED or event == 'Close': break elif event == 'Delete Selected': selected_row_index = values['-TABLE-'][0] if selected_row_index is not None: id_to_delete = data[selected_row_index][0] db.delete_data(id_to_delete) sg.popup('Data deleted successfully!') data = db.read_data() window['-TABLE-'].update(values=data) else: sg.popup('Please select a row to delete.') elif event == 'Update Selected': selected_row_index = values['-TABLE-'][0] if selected_row_index is not None: selected_row_data = data[selected_row_index] update_data_layout(db, selected_row_data) data = db.read_data() window['-TABLE-'].update(values=data) else: sg.popup('Please select a row to update.') elif event == 'Add Data': add_data_layout(db) data = db.read_data() window['-TABLE-'].update(values=data) window.close() # Function to update a database entry def update_data_layout(db, selected_row_data): layout = [ [sg.Text("Name:"), sg.InputText(key='-NAME-', default_text=selected_row_data[1])], [sg.Text("Age:"), sg.InputText(key='-AGE-', default_text=selected_row_data[2])], [sg.Button('Update'), sg.Button('Cancel')] ] window = sg.Window('Update Data', layout) while True: event, values = window.read() if event == sg.WINDOW_CLOSED or event == 'Cancel': break elif event == 'Update': name = values['-NAME-'] age = values['-AGE-'] db.update_data(selected_row_data[0], name, age) sg.popup('Data updated successfully!') break window.close() # Function to delete a database entry by ID def delete_data_layout(db): layout = [ [sg.Text("Enter ID to delete:"), sg.InputText(key='-ID-')], [sg.Button('Delete'), sg.Button('Cancel')] ] window = sg.Window('Delete Data', layout) while True: event, values = window.read() if event == sg.WINDOW_CLOSED or event == 'Cancel': break elif event == 'Delete': id = values['-ID-'] db.delete_data(id) sg.popup('Data deleted successfully!') break window.close() # Main function def main(): # Create instance of Database class db = Database() db.create_table() # Main layout main_layout = [ [sg.Button('Add Data'), sg.Button('View Data'), sg.Button('Delete Data'), sg.Button('Exit')] ] # Create the window window = sg.Window('Database Viewer', main_layout) # Event loop while True: event, values = window.read() if event == sg.WINDOW_CLOSED or event == 'Exit': break elif event == 'Add Data': add_data_layout(db) elif event == 'View Data': view_data_layout(db) elif event == 'Delete Data': delete_data_layout(db) # Close the connection and window db.close_connection() window.close() def make_image(img_path: str): # Load the image using PIL image = Image.open("cat.jpg") image.thumbnail((400, 400)) # Resize the image if needed # Convert the image to bytes img_byte_array = io.BytesIO() image.save(img_byte_array, format="PNG") img_byte_array = img_byte_array.getvalue() return img_byte_array if __name__ == "__main__": main()
Leave a Comment