Sashank
Sashank

Reputation: 580

how to disable a button after being clicked once in pysimplegui?

I want to disable the Start update button in my code

code :

import PySimpleGUI as sg
import time
mylist = ["task 1", "task 2", "task 3", "task 4"]
progressbar = [
    [sg.ProgressBar(len(mylist), orientation='h', size=(51, 10), key='progressbar')]
]
outputwin = [
    [sg.Output(size=(78,20))]
]
layout = [
    [sg.Frame('Progress',layout= progressbar)],
    [sg.Frame('Output', layout = outputwin)],
    [sg.Submit('Start update'),sg.Cancel()]

]
window = sg.Window('Assistant updater', layout)
progress_bar = window['progressbar']
while True:
    event, values = window.read(timeout=10)
    if event == 'Cancel'  or event is None:
        break
    elif event == 'Start update':
        for i,item in enumerate(mylist):
            print(item)
            if item=="Reading files from the internet":
                print('New line started')
            time.sleep(1)
            progress_bar.UpdateBar(i + 1)
        

Can anyone please help me so that I can disable the button after the Start update button so it won't be clicked twice

Upvotes: 2

Views: 11881

Answers (1)

Bhargav Desai
Bhargav Desai

Reputation: 1016

You have to disable button after click until process is complete.

You can try this :

window['Start update'].update(disabled=True)

after your process complete you can enable your button :

window['Start update'].update(disabled=False)

Try This :

while True:
    event, values = window.read(timeout=10)
    if event == 'Cancel'  or event is None:
        break
    elif event == 'Start update':
        window['Start update'].update(disabled=True)
        for i,item in enumerate(mylist):
            print(item)
            if item=="Reading files from the internet":
                print('New line started')
            time.sleep(1)
            progress_bar.UpdateBar(i + 1)
        window['Start update'].update(disabled=False)

Upvotes: 11

Related Questions