Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

How should I change this code so that height and weight can be edited:…

  1. User

    How should I change this code so that height and weight can be edited: #IMPORTS--------------------------------------------------------------- import PySimpleGUI as sg import sqlitedb as db from pokemon_sql import * #IN BOOK IT DOES NOT SAY IMPORT THIS BUT YOU HAVE TO from pokemon_gui_entry import show_pokemon_form

    #VARIABLES/LISTS (I think)-------------------------------------------------

    sg.theme('HotDogStand') FORM_FONT = 'Calibri 16' LABEL_SIZE = (10,1) BUTTON_SIZE = (7,1) BUTTON_PAD = ((10,0),(20,0)) BG = 'black'

    db.db_file = pokemon_database #IN BOOK IT SAYS db.db_file = mission_database BUT CHANGE IT TO db.db_file = pokemon_database pokemon_list = [] pokemon_data = [] data_index = -1

    #WINDOW/LAYOUT-------------------------------------------------

    form = [ [ sg.Text('No.', size=LABEL_SIZE, background_color = BG), sg.Text('', key = 'no', background_color = BG) ], [ sg.Text('Name', size=LABEL_SIZE, background_color = BG), sg.Text('', key = 'reg', background_color = BG) ], [ sg.Text('Nickname', size=LABEL_SIZE, background_color = BG), sg.Text('', key = 'nick', background_color = BG) ], [ sg.Text('Type', size=LABEL_SIZE, background_color = BG), sg.Text('', key = 'type', background_color = BG) ], [ sg.Text('Height', size=LABEL_SIZE, background_color = BG), sg.Text('', key = 'height', background_color = BG) ], [ sg.Text('Weight', size=LABEL_SIZE, background_color = BG), sg.Text('', key = 'weight', background_color = BG) ], [ sg.Text('Desription', size=LABEL_SIZE, background_color = BG), sg.Text('', key = 'desc', background_color = BG) ],

    ]

    layout = [ [ sg.Listbox([], key = 'list', size = (20,10), default_values = None, select_mode = None, enable_events = True, font = FORM_FONT), sg.Column(form, size = (470, 275), vertical_alignment = 'top', element_justification = 'left', background_color = BG ) ],

    [ sg.OK('OK', key = 'ok', pad=BUTTON_PAD, size=BUTTON_SIZE ),
      sg.Button('Add', key = 'add', pad=BUTTON_PAD, size=BUTTON_SIZE ),
      sg.Button('Edit', key = 'edit', pad=BUTTON_PAD, size=BUTTON_SIZE ),
      sg.Button('Delete', key = 'delete', pad=BUTTON_PAD, size=BUTTON_SIZE ),
    ]
    

    ]

    #FUNCTIONS---------------------------------------------------------------------

    def pokemon_data_to_list(): global pokemon_list, data_index pokemon_list = db.query_table('SELECT pokemon_reg FROM pokemon') values = [] for pokemon in pokemon_list: values.append(pokemon[0]) if len(values) > 0 and data_index == -1: data_index = 0 window['list'].update(values = values, set_to_index = data_index)

    def pokemon_data_to_form(reg): #Get values for the selected pokemon global pokemon_data if data_index > -1: if reg == None: reg = pokemon_list[data_index][0] sql_query = ''' SELECT * FROM pokemon INNER JOIN pokemon_type ON pokemon.pokemon_type_id = pokemon_type.pokemon_type_id WHERE pokemon_reg = ''' + '"'+reg+'";' pokemon_data = db.query_table(sql_query)

        window['reg'].update(value = pokemon_data[0][1])
        window['no'].update(value = pokemon_data[0][0])
        window['type'].update(value = pokemon_data[0][4])
        window['weight'].update(value = pokemon_data[0][5])
        window['height'].update(value = pokemon_data[0][6])
        window['nick'].update(value = pokemon_data[0][7])
        window['desc'].update(value = pokemon_data[0][8])
    

    def pokemon_type_name_to_id(s): sql_query = ''' SELECT * FROM pokemon_type WHERE pokemon_type_name = ''' + '"'+s+'";' #IN BOOKLET IT SAYS pokemon_name = IN THE LINE ABOVE IT NEEDS TO BE pokemon_type_name = d = db.query_table(sql_query) print('pokemon_id_from_name = ' ,d) return d[0][0]

    #MISC STUFF-------------------------------------------------------------------

    window = sg.Window("Pokedex", layout, finalize=True, font = FORM_FONT, margins = (10,20))

    pokemon_data_to_list() pokemon_data_to_form(None)

    #MAIN LOOP-------------------------------------------------------------------- print(pokemon_data) while True: event, values = window.read(timeout=10) if not event == 'TIMEOUT': print(event,'|', values) if event in ["Exit", sg.WIN_CLOSED, 'ok']: break

    elif event == 'list':
        pokemon_data_to_form(values['list'][0])
    
    
    
    
    elif event in ['add','edit']:
        if event == 'edit' and data_index > -1:
            data = {}
            data['no'] = pokemon_data[0][0]
            data['pokemon_reg'] = pokemon_data[0][1]
            data['weight'] = pokemon_data[0][5]
            data['pokemon_type'] = pokemon_data[0][4]
            data['height'] = pokemon_data[0][6]
            data['nick'] = pokemon_data[0][7]
            data['desc'] = pokemon_data[0][8]
        else:
            data = None
            pokemon_data = None
        data = show_pokemon_form(data)
    
        if data != None:
            pokemon = []
            pokemon.append(data['pokemon_reg'])
            pokemon.append(pokemon_type_name_to_id(data['pokemon_type']))
    
            if pokemon_data == None:        #add to the list
                sql = """
                    INSERT INTO pokemon(pokemon_reg, pokemon_type_id)
                    VALUES(?,?);
                    """
                id = db.insert_record(sql, pokemon)
                print('Added pokemon record', id)
    
            else:                           #edit the list
                pokemon.append(pokemon_data[0][0])
                sql = """
                    UPDATE pokemon
                    SET pokemon_reg = ?, pokemon_type_id = ? 
                    WHERE pokemon_id = ?
                    """
                id = db.update_record(sql, pokemon)
                print('Updated pokemon record', id)
            pokemon_data_to_list()
            pokemon_data_to_form(data['pokemon_reg'])
    
    
    
    
    elif event == 'delete':
        if pokemon_data != None:
            data = [ pokemon_data[0][0] ]
            sql = """
                DELETE FROM pokemon
                WHERE pokemon_id = ?
                """
            ok = db.update_record(sql, data)
            if ok: print('Record deleted')
    
            pokemon_data_to_list()
            pokemon_data_to_form(None)
    

    window.close()

  2. Assistant

    To allow the height and weight to be edited, you need to make the following changes to the code:

    1. In the pokemon_data_to_form() function, update the lines that update the height and weight values:
    window['weight'].update(value = pokemon_data[0][5])
    window['height'].update(value = pokemon_data[0][6])
    

    Replace them with this code to create input fields for editing:

    window['weight'].update(value = pokemon_data[0][5], disabled=False, enable_events=True)
    window['height'].update(value = pokemon_data[0][6], disabled=False, enable_events=True)
    
    1. Modify the layout to include input fields for height and weight:
    [ sg.Text('Height', size=LABEL_SIZE, background_color = BG),
      sg.Input(key='height', size=(15,1), disabled=True),
      sg.Text('cm')
    ],
    [ sg.Text('Weight', size=LABEL_SIZE, background_color = BG),
      sg.Input(key='weight', size=(15,1), disabled=True),
      sg.Text('kg')
    ],
    
    1. Add event handlers for the height and weight input fields. Replace the elif event in ['add','edit'] block with the following code:
    elif event in ['add','edit']:
        if event == 'edit' and data_index > -1:
            data = {}
            data['no'] = pokemon_data[0][0]
            data['pokemon_reg'] = pokemon_data[0][1]
            data['weight'] = pokemon_data[0][5]
            data['pokemon_type'] = pokemon_data[0][4]
            data['height'] = pokemon_data[0][6]
            data['nick'] = pokemon_data[0][7]
            data['desc'] = pokemon_data[0][8]
        else:
            data = None
            pokemon_data = None
        data = show_pokemon_form(data)
    
        if data != None:
            pokemon = []
            pokemon.append(data['pokemon_reg'])
            pokemon.append(pokemon_type_name_to_id(data['pokemon_type']))
            pokemon.append(data['weight'])
            pokemon.append(data['height'])
    
            if pokemon_data == None:        #add to the list
                sql = """
                    INSERT INTO pokemon(pokemon_reg, pokemon_type_id, weight, height)
                    VALUES(?,?,?,?);
                    """
                id = db.insert_record(sql, pokemon)
                print('Added pokemon record', id)
    
            else:                           #edit the list
                pokemon.append(pokemon_data[0][0])
                sql = """
                    UPDATE pokemon
                    SET pokemon_reg = ?, pokemon_type_id = ?, weight = ?, height = ?
                    WHERE pokemon_id = ?
                    """
                id = db.update_record(sql, pokemon)
                print('Updated pokemon record', id)
            pokemon_data_to_list()
            pokemon_data_to_form(data['pokemon_reg'])
    

    These changes will enable the editing of the height and weight fields in the GUI and update the corresponding values in the database.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.