Created
September 1, 2020 14:47
-
-
Save daguiam/d1310a2f094f1d2fe30e22de8b0884f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from bleak import BleakScanner | |
import matplotlib | |
import PySimpleGUI as sg | |
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import time | |
import json | |
import os | |
import threading | |
import logging | |
import win32gui | |
import win32api | |
def draw_figure(canvas, figure): | |
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas) | |
figure_canvas_agg.draw() | |
figure_canvas_agg.get_tk_widget().pack(side="left", expand=1) | |
return figure_canvas_agg | |
gui_timetout = 100 #ms | |
if __name__ == "__main__": | |
format = "%(asctime)s.%(msecs)03d: %(message)s" | |
logging.basicConfig(format=format, level=logging.INFO, | |
datefmt="%H:%M:%S") | |
logging.info("Main : Started program") | |
sg.theme('SystemDefaultForReal') | |
menu_def = [['File', ['Exit']], | |
['Help', 'About...'], ] | |
column1 = [ | |
[sg.Text("Text")], | |
[sg.Listbox(values=[1,2,3], size=(30, 6), key='listbox_1'),], | |
[sg.Text("Input text: "),sg.Input(key='input_1',size=(10,1), enable_events=True), | |
sg.Button("Set", key='btn_set')], | |
[sg.Canvas(key="-CANVAS-", size=(320, 240)) ], | |
] | |
column2 = [ | |
[sg.Text("Text 2")], | |
[sg.Frame(layout=[ | |
[ sg.Text("More text", key='text_velocity'),], | |
], title='Layout title')], | |
[sg.Text("Text and buttong"),sg.Button("Refresh", key='btn_refresh_devices')], | |
[sg.Button("Play", key='btn_play'),sg.Button("Stop", key='btn_stop'), | |
sg.StatusBar( text=f'Stopped', size=(10,1), justification='left', visible=True, key='play_status_bar' )] | |
] | |
# Define the window layout | |
layout = [ | |
[sg.Menu(menu_def, tearoff=True)], | |
[sg.Column(column1),sg.VerticalSeparator(), sg.Column(column2)], | |
[sg.StatusBar( text=f'', size=(50,1), justification='left', visible=True, key='status_bar' )], | |
# [sg.OK(), sg.Cancel()] | |
] | |
# icon_base64 = | |
# Create the form and show it without the plot | |
title_window = "Application title" | |
window = sg.Window( | |
title_window, | |
layout, | |
location=(0, 0), | |
finalize=True, | |
element_justification="center", | |
icon = sg.DEFAULT_BASE64_ICON, | |
) | |
window['status_bar'].expand(expand_x=True, expand_y=True) | |
if 0: | |
# initialize plot | |
fig_canvas, ax = plt.subplots(1,1, figsize=(4,2)) | |
t = np.linspace(0,2, 100) | |
y = np.sin(2*np.pi*10*t) | |
plt.plot(t,y) | |
plt.xlabel('t [s]') | |
plt.ylabel('A') | |
plt.subplots_adjust(hspace=0.4, left=0.20, bottom=0.25) | |
window["-CANVAS-"].set_tooltip('Plot canvas') | |
agg_figure = draw_figure(window["-CANVAS-"].TKCanvas, fig_canvas) | |
while True: | |
event, values = window.read(timeout=gui_timetout) | |
# if event == sg.WIN_CLOSED or event == 'Exit': | |
if event == sg.WIN_CLOSED or event in ('Exit', None, 'Cancel'): | |
logging.info("Main : Exiting") | |
break | |
# Refresh plot | |
if 0: | |
axes = fig_canvas.axes | |
# clar first axis | |
ax = axes[0] | |
ax.cla() | |
t = np.linspace(0,2, 100) | |
y = np.random.rand(len(t)) | |
ax.plot(t,y) | |
ax.set_xlabel('x') | |
ax.set_ylabel('y') | |
agg_figure.draw() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment