From 60a6c07b7aac5bca9932a3b8f2d31cfe019987c0 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Fri, 24 Aug 2018 08:36:45 -0400 Subject: [PATCH 1/6] Cleanup code --- Demo_MIDI_Player.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/Demo_MIDI_Player.py b/Demo_MIDI_Player.py index 3da9b17d..3e0e81b5 100644 --- a/Demo_MIDI_Player.py +++ b/Demo_MIDI_Player.py @@ -56,23 +56,19 @@ class PlayerGUI(): image_next = './ButtonGraphics/Next.png' image_exit = './ButtonGraphics/Exit.png' - self.TextElem = g.T('Song loading....', size=(85, 5 + NumFiles), font=("Helvetica", 14), auto_size_text=False) - form = g.FlexForm('MIDI File Player', default_element_size=(30, 1),font=("Helvetica", 25)) + self.TextElem = g.T('Song loading....', size=(85,5 + NumFiles), font=("Helvetica", 14), auto_size_text=False) + form = g.FlexForm('MIDI File Player', default_element_size=(30,1),font=("Helvetica", 25)) layout = [ - [g.T('MIDI File Player', size=(30, 1), font=("Helvetica", 25))], + [g.T('MIDI File Player', size=(30,1), font=("Helvetica", 25))], [self.TextElem], [g.ReadFormButton('PAUSE', button_color=g.TRANSPARENT_BUTTON, - image_filename=image_pause, image_size=(50,50),image_subsample=2, border_width=0, - font=("Helvetica", 15), size=(10, 2)), g.T(' ' * 3), + image_filename=image_pause, image_size=(50,50),image_subsample=2, border_width=0), g.T(' '), g.ReadFormButton('NEXT', button_color=g.TRANSPARENT_BUTTON, - image_filename=image_next, image_size=(50,50),image_subsample=2, border_width=0, - size=(10, 2), font=("Helvetica", 15)), g.T(' ' * 3), + image_filename=image_next, image_size=(50,50),image_subsample=2, border_width=0), g.T(' '), g.ReadFormButton('Restart Song', button_color=g.TRANSPARENT_BUTTON, - image_filename=image_restart, image_size=(50,50), image_subsample=2,border_width=0, - size=(10, 2), font=("Helvetica", 15)), g.T(' ' * 3), - g.T(' '*2), g.SimpleButton('EXIT', button_color=g.TRANSPARENT_BUTTON, - image_filename=image_exit, image_size=(50,50), image_subsample=2,border_width=0, - size=(10, 2), font=("Helvetica", 15))] + image_filename=image_restart, image_size=(50,50), image_subsample=2, border_width=0), g.T(' '), + g.SimpleButton('EXIT', button_color=g.TRANSPARENT_BUTTON, + image_filename=image_exit, image_size=(50,50), image_subsample=2, border_width=0,)] ] form.LayoutAndRead(layout, non_blocking=True) @@ -117,15 +113,13 @@ def main(): ''' return int(round(time.time() * 1000)) - g.SetOptions(border_width=1, element_padding=(4, 6), font=("Helvetica", 10), button_color=('white', g.BLUES[0]), - progress_meter_border_depth=1, slider_border_width=1) pback = PlayerGUI() button, values = pback.PlayerChooseSongGUI() if button != 'PLAY!': g.MsgBoxCancel('Cancelled...\nAutoclose in 2 sec...', auto_close=True, auto_close_duration=2) exit(69) - if values['device'] is not None: + if values['device']: midi_port = values['device'][0] else: g.MsgBoxCancel('No devices found\nAutoclose in 2 sec...', auto_close=True, auto_close_duration=2) From 595b3c09938752d82c97e64abdba47dc419315cd Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Fri, 24 Aug 2018 12:20:01 -0400 Subject: [PATCH 2/6] Chatterbot front-end. Machine Learning --- Demo_Chatterbot.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Demo_Chatterbot.py diff --git a/Demo_Chatterbot.py b/Demo_Chatterbot.py new file mode 100644 index 00000000..a0e69844 --- /dev/null +++ b/Demo_Chatterbot.py @@ -0,0 +1,38 @@ +import PySimpleGUI as gui +from chatterbot import ChatBot +import chatterbot.utils + +''' +Demo_Chatterbot.py +A GUI wrapped arouind the Chatterbot package. +The GUI is used to show progress bars during the training process and +to collect user input that is sent to the chatbot. The reply is displayed in the GUI window +''' + +# redefine the chatbot text based progress bar with a graphical one +def print_progress_bar(description, iteration_counter, total_items, progress_bar_length=20): + gui.EasyProgressMeter(description, iteration_counter, total_items) + +chatterbot.utils.print_progress_bar = print_progress_bar + +chatbot = ChatBot('Ron Obvious', trainer='chatterbot.trainers.ChatterBotCorpusTrainer') + +# Train based on the english corpus +chatbot.train("chatterbot.corpus.english") + +################# GUI ################# +with gui.FlexForm('Chat Window', auto_size_text=True, default_element_size=(30, 2)) as form: + layout = [ [gui.Output(size=(80, 20))], + [gui.Multiline(size=(70, 5), enter_submits=True), + gui.ReadFormButton('SEND', bind_return_key=True), gui.SimpleButton('EXIT')]] + + form.Layout(layout) + # ---===--- Loop taking in user input and using it to query HowDoI web oracle --- # + while True: + button, (value,) = form.Read() + if button != 'SEND': + break + print(value.rstrip()) + # send the user input to chatbot to get a response + response = chatbot.get_response(value.rstrip()) + print(response) \ No newline at end of file From 17d87870e7960d02f3c845125cf9b286d5da2101 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Fri, 24 Aug 2018 21:23:57 -0400 Subject: [PATCH 3/6] "Dashboard" design for progress meters. Dashboard type of design that includes 20 Progress Meters. Able to see the progess meters after they have competed. --- Demo_Chatterbot.py | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/Demo_Chatterbot.py b/Demo_Chatterbot.py index a0e69844..15cfa808 100644 --- a/Demo_Chatterbot.py +++ b/Demo_Chatterbot.py @@ -1,4 +1,4 @@ -import PySimpleGUI as gui +import PySimpleGUI as g from chatterbot import ChatBot import chatterbot.utils @@ -9,10 +9,34 @@ The GUI is used to show progress bars during the training process and to collect user input that is sent to the chatbot. The reply is displayed in the GUI window ''' -# redefine the chatbot text based progress bar with a graphical one -def print_progress_bar(description, iteration_counter, total_items, progress_bar_length=20): - gui.EasyProgressMeter(description, iteration_counter, total_items) +# Create the 'Trainer GUI' +MAX_PROG_BARS = 20 +bars = [] +texts = [] +training_layout = [[g.T('TRAINING PROGRESS', size=(20,1), font=('Helvetica', 17))]] +for i in range(MAX_PROG_BARS): + bars.append(g.ProgressBar(100, size=(30, 5))) + texts.append(g.T(' '*20)) + training_layout += [[texts[i], bars[i]]] +training_form = g.FlexForm('Training') +training_form.Layout(training_layout) +current_bar = 0 + +# callback function for training runs +def print_progress_bar(description, iteration_counter, total_items, progress_bar_length=20): + global current_bar + global bars + global texts + global training_form + # update the form and the bars + training_form.ReadNonBlocking() + bars[current_bar].UpdateBar(iteration_counter, max=total_items) + texts[current_bar].Update(description) + if iteration_counter == total_items: + current_bar += 1 + +# redefine the chatbot text based progress bar with a graphical one chatterbot.utils.print_progress_bar = print_progress_bar chatbot = ChatBot('Ron Obvious', trainer='chatterbot.trainers.ChatterBotCorpusTrainer') @@ -21,10 +45,10 @@ chatbot = ChatBot('Ron Obvious', trainer='chatterbot.trainers.ChatterBotCorpusTr chatbot.train("chatterbot.corpus.english") ################# GUI ################# -with gui.FlexForm('Chat Window', auto_size_text=True, default_element_size=(30, 2)) as form: - layout = [ [gui.Output(size=(80, 20))], - [gui.Multiline(size=(70, 5), enter_submits=True), - gui.ReadFormButton('SEND', bind_return_key=True), gui.SimpleButton('EXIT')]] +with g.FlexForm('Chat Window', auto_size_text=True, default_element_size=(30, 2)) as form: + layout = [[g.Output(size=(80, 20))], + [g.Multiline(size=(70, 5), enter_submits=True), + g.ReadFormButton('SEND', bind_return_key=True), g.SimpleButton('EXIT')]] form.Layout(layout) # ---===--- Loop taking in user input and using it to query HowDoI web oracle --- # From 4a0d7b815ddb82c8de78258f0d54b7326b765229 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Fri, 24 Aug 2018 22:44:52 -0400 Subject: [PATCH 4/6] Correct exiting from application is user closes windows --- Demo_Chatterbot.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Demo_Chatterbot.py b/Demo_Chatterbot.py index 15cfa808..6697335b 100644 --- a/Demo_Chatterbot.py +++ b/Demo_Chatterbot.py @@ -30,8 +30,11 @@ def print_progress_bar(description, iteration_counter, total_items, progress_bar global texts global training_form # update the form and the bars - training_form.ReadNonBlocking() - bars[current_bar].UpdateBar(iteration_counter, max=total_items) + button, values = training_form.ReadNonBlocking() + if button is None and values is None: + exit(69) + if bars[current_bar].UpdateBar(iteration_counter, max=total_items) is False: + exit(69) texts[current_bar].Update(description) if iteration_counter == total_items: current_bar += 1 From 4119ea8b5cfb2a077681b0998b8242fef7a5a0d8 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Fri, 24 Aug 2018 22:47:21 -0400 Subject: [PATCH 5/6] Slider - range can be changed using Update, Progress Bars - Max value c an be changed on the fly when calling UpdateBar. Fixed bug when multiple bars on one form --- PySimpleGUI.py | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/PySimpleGUI.py b/PySimpleGUI.py index 0fc5f76a..0bc75dc5 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -512,24 +512,21 @@ class TKProgressBar(): if orientation[0].lower() == 'h': s = ttk.Style() s.theme_use(style) - s.configure("my.Horizontal.TProgressbar", background=BarColor[0], troughcolor=BarColor[1], troughrelief=relief, borderwidth=border_width, thickness=width) - self.TKProgressBarForReal = ttk.Progressbar(root, maximum=self.Max, style='my.Horizontal.TProgressbar', length=length, orient=tk.HORIZONTAL, mode='determinate') - # self.TKCanvas = tk.Canvas(root, width=length, height=width, highlightt=highlightt, relief=relief, borderwidth=border_width) - # self.TKRect = self.TKCanvas.create_rectangle(0, 0, -(length * 1.5), width * 1.5, fill=BarColor[0], tags='bar') - # self.canvas.pack(padx='10') + s.configure(str(length)+str(width)+"my.Horizontal.TProgressbar", background=BarColor[0], troughcolor=BarColor[1], troughrelief=relief, borderwidth=border_width, thickness=width) + self.TKProgressBarForReal = ttk.Progressbar(root, maximum=self.Max, style=str(length)+str(width)+'my.Horizontal.TProgressbar', length=length, orient=tk.HORIZONTAL, mode='determinate') else: - # s = ttk.Style() - # s.theme_use('clam') - # s.configure('Vertical.mycolor.progbar', forground=BarColor[0], background=BarColor[1]) s = ttk.Style() s.theme_use(style) - s.configure("my.Vertical.TProgressbar", background=BarColor[0], troughcolor=BarColor[1], troughrelief=relief, borderwidth=border_width, thickness=width) - self.TKProgressBarForReal = ttk.Progressbar(root, maximum=self.Max, style='my.Vertical.TProgressbar', length=length, orient=tk.VERTICAL, mode='determinate') - # self.TKCanvas = tk.Canvas(root, width=width, height=length, highlightt=highlightt, relief=relief, borderwidth=border_width) - # self.TKRect = self.TKCanvas.create_rectangle(width * 1.5, 2 * length + 40, 0, length * .5, fill=BarColor[0], tags='bar') - # self.canvas.pack() + s.configure(str(length)+str(width)+"my.Vertical.TProgressbar", background=BarColor[0], troughcolor=BarColor[1], troughrelief=relief, borderwidth=border_width, thickness=width) + self.TKProgressBarForReal = ttk.Progressbar(root, maximum=self.Max, style=str(length)+str(width)+'my.Vertical.TProgressbar', length=length, orient=tk.VERTICAL, mode='determinate') - def Update(self, count): + def Update(self, count, max=None): + if max is not None: + self.Max = max + try: + self.TKProgressBarForReal.config(maximum=max) + except: + return False if count > self.Max: return False try: self.TKProgressBarForReal['value'] = count @@ -757,13 +754,13 @@ class ProgressBar(Element): self.BorderWidth = border_width if border_width else DEFAULT_PROGRESS_BAR_BORDER_WIDTH self.Relief = relief if relief else DEFAULT_PROGRESS_BAR_RELIEF self.BarExpired = False - super().__init__(ELEM_TYPE_PROGRESS_BAR, scale, size, auto_size_text) + super().__init__(ELEM_TYPE_PROGRESS_BAR, scale=scale, size=size, auto_size_text=auto_size_text) return - def UpdateBar(self, current_count): + def UpdateBar(self, current_count, max=None): if self.ParentForm.TKrootDestroyed: return False - self.TKProgressBar.Update(current_count) + self.TKProgressBar.Update(current_count, max=max) try: self.ParentForm.TKroot.update() except: @@ -840,8 +837,11 @@ class Slider(Element): super().__init__(ELEM_TYPE_INPUT_SLIDER, scale=scale, size=size, font=font, background_color=background_color, text_color=text_color, key=key) return - def Update(self, value): + def Update(self, value, range=(None, None)): self.TKIntVar.set(value) + if range != (None, None): + self.TKScale.config(from_ = range[0], to_ = range[1]) + def __del__(self): super().__del__() @@ -1749,6 +1749,7 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form): if text_color is not None and text_color != COLOR_SYSTEM_DEFAULT: tkscale.configure(fg=text_color) tkscale.pack(side=tk.LEFT, padx=element.Pad[0],pady=element.Pad[1]) + element.TKScale = tkscale #............................DONE WITH ROW pack the row of widgets ..........................# # done with row, pack the row of widgets tk_row_frame.grid(row=row_num+2, sticky=tk.NW, padx=DEFAULT_MARGINS[0]) From 60034cd168c575ddb1fcf779e38c269ce14b085c Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Fri, 24 Aug 2018 23:32:12 -0400 Subject: [PATCH 6/6] Progress bar - decrement num windows if update fails (RISKY CHANGE) More battling over the number of open windows. Hopefully won't cause lots of problems! --- PySimpleGUI.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PySimpleGUI.py b/PySimpleGUI.py index 0bc75dc5..ba25b271 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -764,7 +764,7 @@ class ProgressBar(Element): try: self.ParentForm.TKroot.update() except: - # _my_windows.Decrement() + _my_windows.Decrement() return False return True