comment a line of code if checkbox checked Pysimplegui


how can i comment/uncomment a line of code by checking and unchecking a checkbox in python with PySimpleGUI?

also i don’t know if i wrote the code in correct way but i’m just trying to comment a line of code by checking the checkbox

any other way to do it is also fix my problem

This is my code

layout = [[sg.Text('Choose Options'))],
            [sg.Checkbox('Save Posts',key="save-ed")],
                 [sg.Submit('Next')) ,sg.Cancel("Cancel"))] ]

window = sg.Window('my bot', layout, icon="logo.ico")
event, values =
while True:
    event, values =
    if event == sg.WIN_CLOSED or event == "Cancel":
    elif values['save-ed'] == True:
        save_input = ['usersave']
    elif values['save-ed'] == False:
        save_input = ['#usersave']

and this is the code which i want to comment or uncomment with checkbox

            save_input = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[1]/span[4]/div/div/button/div')
  except NoSuchElementException:


Following code show how to stop a thread to update time by a checkbox.

from datetime import datetime
from time import sleep
import threading
import PySimpleGUI as sg

def clock(window):
    now = None
    while timer:
        new ="%Y-%m-%d %H:%M:%S")
        if new != now:
            now = new
            if flag:
                window.write_event_value('CLOCK', now)

sg.set_options(font=("Courier New", 16))

layout = [
    [sg.Text("", size=(0, 1), key='TIME')],
    [sg.Checkbox("Time ON", default=True, enable_events=True, key='TIME ON')],
window = sg.Window('Title', layout, finalize=True)
timer, flag = True, True
threading.Thread(target=clock, args=(window,), daemon=True).start()
while True:

    event, values =
    if event == sg.WINDOW_CLOSED:
        timer = False
    elif event == 'TIME ON':
        flag = values[event]
    elif event == 'CLOCK':


Answered By – Jason Yang

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published