[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.
Answer # 1
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.
- python error using jupyter notebook
- the prediction result of fx chart by lstm of rnn using keras in python deviates from the actual data
- python - i want to create a button with tkinter and call a function
- python - click does not work with the button tag obtained with selenium
- i want to get the product information of the application base using python
- python - i want to make the execution result successful by using to_excel method
- python - how to set tkinter button coordinates containing images with plase
- python - run sox using subprocess
- python - how to pass to export_file_path using to_excel
- python 3x - principal component analysis using python (with data)
- python - cannot apply design using django form
- about attendance scheduling using python pulp
- [python3x] i want to sort by using multiple keywords in a pandas data frame and then add the relevant keywords to one column
- how to create a layout using classes in wxpython
- python - i want to make my own image classifier using this site
- can't get source code of local file using python＋selenium＋ie driver
- python - i want to upload a file using modelform
- python 3x - i want to perform regression analysis using linearregression please tell me about the input data
- python 3x - using turtle graphics returns to the macos login screen
- python 3x - typeerror: 'method' object is not subscriptable
- python - you may need to restart the kernel to use updated packages error
- xcode - pod install [!] no `podfile 'found in the project directory
- android studio - unresolved reference comes out in kotlin
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- python - importerror: cannot import name md5 error cannot be resolved