Home>

[What I want to achieve]
I am using PySimpleGUI to create a GUI that executes a specified action at regular intervals when the start button is pressed. It is implemented with the code shown below, but currently it is realized by entering Ctrl+C at the command prompt to interrupt the specified processing.
I would like to implement this interruption method using the GUI of the button.

import PySimpleGUI as sg
import pyperclip
import subprocess
import time
import threading
import pyautogui as pgui
#Variable declaration***************
# Omitted
#************************
def set_pisi:
    # Omitted
def update(i):
    #Repeat processing (omitted)
sg.theme('LightGray1')
layout = [(sg.Text('Switching time approx.', font=('Kozuka Gothic', 12)), sg.InputText(default_text='5', size=(2,3), key='enter_time') ,sg.Text('second (minimum 2)', font=('Kozuka Gothic', 12))],
           (sg.Text('Number of switching tabs', font=('Kozuka Gothic', 12)), sg.InputText(default_text='2', size=(2,3), key='tab_number'), sg .Text('(up to 4)', font=('Kozuka Gothic', 12))],
           [sg.Text('The initial value of the coordinate is 2 tabs for switching and the following is assumed', font=('Kozuka Gothic',10),text_color='#000000')],
           [sg.Text(' → total number of Tabs 4-5: switching Tab=3,4', font=('Kozuka Gothic',10))],
           [sg.Text('Ctrl + C exits the loop on the command prompt', font=('Kozuka Gothic',10),text_color='#000000')],
           [sg.Button('start',key='start'),sg.Button('coordinate setting',key='posi')]]
window = sg.Window('Enter/Tag auto', layout,resizable=True,size=(300,180))
while True:
    event, values ​​= window.read()
    if event =='posi':
       # Omitted
    if event =='start':
        # Omitted
        Change value to enter #loop
        auto = True
    if event == None:
        break
    if auto:
       try:
A loop for repeating the processing below #
         #I want to exit this loop with a button click
         while True:
           t = threading.Thread(target = update(tab_posi))
           t.start()
           next_time = ((base_time-time.time()) %interval) or interval
           time.sleep(next_time)
           # Omitted
       #Ctrl+C exits the loop
       except KeyboardInterrupt:
         print("out roop")
         auto = False
window.close()


It is best to improve it based on this code,
If it can't be achieved without fundamental changes, I'll recreate it from scratch.
Please professor!

  • Answer # 1

    With sg.one_line_progress_meter,
    You can display a progress bar that can be used for loop break conditions.
    (However, if time.sleep is too long, it will not move at any timing)

    Also check the timeout option specified in the window.read option.
    It sends events at regular intervals, so you can treat it like a timer.

    If you want to make structural improvements,
    Instead of creating a thread each time in a loop, loop processing in the thread,
    It is good to exit the loop condition on the thread side with a flag etc. at the time of suspension.

    Reference: I want to be able to interrupt the process with the debug window of Pysimplegui or the button of the progress meter.