Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 512
Sashank Avatar asked Dec 04 '25 04:12

Sashank


1 Answers

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)
like image 61
Bhargav Desai Avatar answered Dec 06 '25 18:12

Bhargav Desai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!