如果选中复选框,则注释一行代码 Pysimplegui

comment a line of code if checkbox checked Pysimplegui

如何使用 PySimpleGUI 通过选中和取消选中 python 中的复选框来 comment/uncomment 一行代码?

我也不知道我是否以正确的方式编写了代码,但我只是想通过选中复选框来评论一行代码

任何其他方法也可以解决我的问题

这是我的代码

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']

这是我想用复选框评论或取消评论的代码

 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

以下代码显示如何通过复选框停止线程以更新时间。

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()

根据@jason-yang之前的评论和回答 我明白了并像这样更改了代码并解决了我的问题

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
    if event == "save posts":
        if values['save-ed'] == True:
            save_input = values['usersave']
        elif values['save-ed'] == False:
            save_input = values['#usersave']

其余代码似乎是正确的,我保持不变

 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