Use the NEW Timeout capability for timing... no more sleeps
This commit is contained in:
		
							parent
							
								
									aa40a73266
								
							
						
					
					
						commit
						0645b49624
					
				
					 3 changed files with 29 additions and 35 deletions
				
			
		|  | @ -49,7 +49,7 @@ def main(): | |||
|                        justification='center', key='text')], | ||||
|                  [sg.Text('', size=(30, 8), font=('Courier New', 12),text_color='white', justification='left', key='processes')], | ||||
|                  [sg.Exit(button_color=('white', 'firebrick4'), pad=((15,0), 0), size=(9,1)), | ||||
|                   sg.Spin([x+1 for x in range(10)], 1, key='spin')],] | ||||
|                   sg.Spin([x+1 for x in range(10)], 3, key='spin')],] | ||||
| 
 | ||||
|     window = sg.Window('CPU Utilization', | ||||
|                        no_titlebar=True, | ||||
|  | @ -59,25 +59,19 @@ def main(): | |||
|     # start cpu measurement thread | ||||
|     thread = Thread(target=CPU_thread,args=(None,)) | ||||
|     thread.start() | ||||
|     timeout_value = 1             # make first read really quick | ||||
|     g_interval = 1 | ||||
|     # ----------------  main loop  ---------------- | ||||
|     while (True): | ||||
|         # --------- Read and update window -------- | ||||
|         event, values = window.ReadNonBlocking() | ||||
| 
 | ||||
|         event, values = window.Read(timeout=timeout_value, timeout_key='Timeout') | ||||
|         # --------- Do Button Operations -------- | ||||
|         if values is None or event == 'Exit': | ||||
|         if event is None or event == 'Exit': | ||||
|             break | ||||
|         try: | ||||
|             g_interval = int(values['spin']) | ||||
|         except: | ||||
|             g_interval = 1 | ||||
| 
 | ||||
|         # cpu_percent = psutil.cpu_percent(interval=interval)       # if don't wan to use a task | ||||
|         timeout_value = int(values['spin']) * 1000 | ||||
| 
 | ||||
|         cpu_percent = g_cpu_percent | ||||
| 
 | ||||
|         # let the GUI run ever 700ms regardless of CPU polling time. makes window be more responsive | ||||
|         time.sleep(.7) | ||||
| 
 | ||||
|         display_string = '' | ||||
|         if g_procs: | ||||
|             # --------- Create list of top % CPU porocesses -------- | ||||
|  | @ -93,13 +87,10 @@ def main(): | |||
|             for proc, cpu in top_sorted: | ||||
|                 display_string += '{:2.2f} {}\n'.format(cpu/10, proc) | ||||
| 
 | ||||
| 
 | ||||
|         # --------- Display timer in window -------- | ||||
|         # --------- Display timer and proceses in window -------- | ||||
|         window.FindElement('text').Update('CPU {}'.format(cpu_percent)) | ||||
|         window.FindElement('processes').Update(display_string) | ||||
| 
 | ||||
|     # Broke out of main loop. Close the window. | ||||
|     window.CloseNonBlocking() | ||||
|     g_exit = True | ||||
|     thread.join() | ||||
| 
 | ||||
|  |  | |||
|  | @ -14,28 +14,28 @@ sg.ChangeLookAndFeel('Black') | |||
| layout = [[sg.Text('CPU Utilization')], | ||||
|           [sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='_text_')], | ||||
|           [sg.Exit(button_color=('white', 'firebrick4'), pad=((15, 0), 0), size=(9,1)), | ||||
|            sg.Spin([x + 1 for x in range(10)], 1, key='_spin_')]] | ||||
|            sg.Spin([x + 1 for x in range(10)], 3, key='_spin_')]] | ||||
| 
 | ||||
| # Layout the rows of the Window | ||||
| window = sg.Window('CPU Meter', | ||||
|                    no_titlebar=True, | ||||
|                    keep_on_top=True, | ||||
|                    grab_anywhere=True).Layout(layout) | ||||
|                    grab_anywhere=True).Layout(layout).Finalize() | ||||
| 
 | ||||
| # ----------------  main loop  ---------------- | ||||
| interval = 10            # For the first one, make it quick | ||||
| while (True): | ||||
|     # --------- Read and update window -------- | ||||
|     event, values = window.ReadNonBlocking() | ||||
| 
 | ||||
|     event, values = window.Read(timeout=interval) | ||||
|     # --------- Do Button Operations -------- | ||||
|     if values is None or event == 'Exit': | ||||
|     if event is None or event == 'Exit': | ||||
|         break | ||||
|     try: | ||||
|         interval = int(values['_spin_']) | ||||
|     except: | ||||
|         interval = 1 | ||||
| 
 | ||||
|     cpu_percent = psutil.cpu_percent(interval=interval) | ||||
|     interval = int(values['_spin_'])*1000 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     cpu_percent = psutil.cpu_percent(interval=1) | ||||
| 
 | ||||
|     # --------- Display timer in window -------- | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,6 +1,5 @@ | |||
| #!/usr/bin/env python | ||||
| import sys | ||||
| import sys | ||||
| if sys.version_info[0] >= 3: | ||||
|     import PySimpleGUI as sg | ||||
| else: | ||||
|  | @ -8,7 +7,15 @@ else: | |||
| import time | ||||
| 
 | ||||
| """ | ||||
|  Timer Desktop Widget Creates a floating timer that is always on top of other windows You move it by grabbing anywhere on the window Good example of how to do a non-blocking, polling program using PySimpleGUI Can be used to poll hardware when running on a Pi     NOTE - you will get a warning message printed when you exit using exit button. | ||||
|  Timer Desktop Widget Creates a floating timer that is always on top of other windows You move it by grabbing anywhere on the window Good example of how to do a non-blocking, polling program using SimpleGUI Can be used to poll hardware when running on a Pi | ||||
|   | ||||
|  While the timer ticks are being generated by PySimpleGUI's "timeout" mechanism, the actual value | ||||
|   of the timer that is displayed comes from the system timer, time.time().  This guarantees an | ||||
|   accurate time value is displayed regardless of the accuracy of the PySimpleGUI timer tick. If | ||||
|   this design were not used, then the time value displayed would slowly drift by the amount of time | ||||
|   it takes to execute the PySimpleGUI read and update calls (not good!)      | ||||
|   | ||||
|  NOTE - you will get a warning message printed when you exit using exit button. | ||||
|  It will look something like: invalid command name \"1616802625480StopMove\" | ||||
| """ | ||||
| 
 | ||||
|  | @ -32,14 +39,14 @@ start_time = int(round(time.time() * 100)) | |||
| while (True): | ||||
|     # --------- Read and update window -------- | ||||
|     if not paused: | ||||
|         event, values = window.ReadNonBlocking() | ||||
|         event, values = window.Read(timeout=10) | ||||
|         current_time = int(round(time.time() * 100)) - start_time | ||||
|     else: | ||||
|         event, values = window.Read() | ||||
|     if event == 'button': | ||||
|         event = window.FindElement(event).GetText() | ||||
|     # --------- Do Button Operations -------- | ||||
|     if values is None or event == 'Exit': | ||||
|     if event is None or event == 'Exit':        # ALWAYS give a way out of program | ||||
|         break | ||||
|     if event is 'Reset': | ||||
|         start_time = int(round(time.time() * 100)) | ||||
|  | @ -60,9 +67,5 @@ while (True): | |||
|     window.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60, | ||||
|                                                                   (current_time // 100) % 60, | ||||
|                                                                   current_time % 100)) | ||||
|     time.sleep(.01) | ||||
| 
 | ||||
| # --------- After loop -------- | ||||
| 
 | ||||
| # Broke out of main loop. Close the window. | ||||
| window.CloseNonBlocking() | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue