Python Forum
PySimpleGUI Bugging out
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PySimpleGUI Bugging out
#8
Remaking the window just to update the contents is not acceptable. The flash will annoy your users and your program has to be much more compilated because it is taking on window manager responsibilities. Updating the window without redrawing is easy. Far easier than what you are doing. Look at the pattern 2B in the cookbook. Here's an example using your code base.
import random
import PySimpleGUI as sg

def update_open_tickets(window, open_tickets):
    window['-TICKETS_LIST-'].update(open_tickets)

def open_a_ticket(window, open_tickets):
    open_tickets.append(random.choice(['Billie Eilish', 'Billy Joel', 'Taylor Swift', 'BTS', 'Bob Dylan']))
    update_open_tickets(window, open_tickets)

def select_tickets(window, open_tickets):
    if new_tickets := window['-TICKETS_LIST-'].get():
        open_tickets.extend(new_tickets)
        update_open_tickets(window, open_tickets)

def adminmain():
    ticket_column = [
        [sg.B("Select", expand_x=True, expand_y=True, key='-TICKETS_SELECT-')],
        [sg.B("Refresh", expand_x=True, expand_y=True, key='-TICKETS_REFRESH-')]
    ]
    ticket_tab = [
        [sg.Listbox(values=[], size=(40, 10), key="-TICKETS_LIST-"), sg.Column(ticket_column, expand_y=True)]
    ]
    equipment_tab = [
        [sg.T('Nothing to see here.  Move along.')]
    ]

    layout = [[sg.TabGroup([[sg.Tab('Equipment', equipment_tab), sg.Tab('Open Tickets', ticket_tab)]])]]
    window = sg.Window('IT Portal', layout, finalize=True)

    open_tickets = ['Bruce Springsteen']
    update_open_tickets(window, open_tickets)

    while True:
        event, values = window.read()
        if event == sg.WIN_CLOSED:
            break
        elif event == "-TICKETS_REFRESH-":
            open_a_ticket(window, open_tickets)
        elif event == "-TICKETS_SELECT-":
            select_tickets(window, open_tickets)

adminmain()
Pressing the refresh button adds a random ticket to the list. No getting the window position. No redrawing the window. No forcing the user to re-select the tab after you redraw the window. Simple.

Your event loop looks terrible. If processing an event requires many lines of code, write a function and call the function from the loop. Event code in the loop should be 5 lines tops. You can get away with lots of code for processing an event when you only have a couple of events, Event then I think it is a bad idea to have one monster "do it all" function. A side benefit of using functions to process your events is you can use the same functions to populate your lists and other controls when you first draw your windows. Notice that I used an empty list in the ticket layout listbox and updated the listbox after the window was drawn. Currently your layouts have a lot of code because they are populating all the controls with info from the database. You should move all that code into functions that you can use for initializing and updating the window, create the layout using empty values, and then call you functions to initialize the window.

Another thing about layouts. Use keys for everything. Notice how easy it is to find events in my event loop because of how the events are named. Think of how this would look if all your user ticket tab controls had keys that started with '-USER_' and all the total equipment tab controls had keys that started with '-EQUIP_'. It would make it really easy to find the event code associated with a particular control on a particular tab. Far better than 'select'. select what? select where?

Another thing about layouts. You have a lot of blank labels that you use for spacers. Often vertical, but sometimes horizontal to make controls line up on different rows. That is going to be a problem if you ever change your font and it may be sensitive to changing themes. If you need vertically aligned widgets, use columns, or better yet rethink your layout so vertical alignment is not needed. Read through the documents and become familiar with the command line arguments and methods for each element. Most elements share the same arguments and methods so this won't take lone.

Stop using so many variable names. Why do you have window0, window1 when you only ever have 1 window? window0 is a bad variable name. main_window, login_dialog, error_message are meaningful names. They say what they are. window0 and window1? What do they mean?
Reply


Messages In This Thread
PySimpleGUI Bugging out - by jamesaarr - Sep-22-2021, 03:02 PM
RE: PySimpleGUI Bugging out - by deanhystad - Sep-22-2021, 05:26 PM
RE: PySimpleGUI Bugging out - by jamesaarr - Sep-23-2021, 09:01 AM
RE: PySimpleGUI Bugging out - by deanhystad - Sep-23-2021, 12:57 PM
RE: PySimpleGUI Bugging out - by jamesaarr - Sep-23-2021, 01:47 PM
RE: PySimpleGUI Bugging out - by deanhystad - Sep-23-2021, 01:51 PM
RE: PySimpleGUI Bugging out - by jamesaarr - Sep-29-2021, 10:47 AM
RE: PySimpleGUI Bugging out - by deanhystad - Sep-29-2021, 06:47 PM
RE: PySimpleGUI Bugging out - by deanhystad - Sep-30-2021, 07:57 PM
RE: PySimpleGUI Bugging out - by deanhystad - Oct-05-2021, 07:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PySimpleGUI Try Except jamesaarr 1 1,985 Nov-18-2021, 02:02 PM
Last Post: jamesaarr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020