comment a line of code if checkbox checked Pysimplegui

Issue

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 = window.read()
window.close()
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == "Cancel":
        break
    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

 try:
            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')
            save_input.click()
            sleep(randint(4,5))
  except NoSuchElementException:
       pass

Solution

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 = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        if new != now:
            now = new
            if flag:
                window.write_event_value('CLOCK', now)
        sleep(0.1)

sg.theme("DarkBlue3")
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 = window.read()
    if event == sg.WINDOW_CLOSED:
        timer = False
        break
    elif event == 'TIME ON':
        flag = values[event]
    elif event == 'CLOCK':
        window['TIME'].update(values[event])

window.close()

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