From 0c37488dd3e204271c46863671839bea5551774c Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 4 Sep 2018 00:30:29 -0400 Subject: [PATCH 1/4] Font Siszer demo updated --- Demo_Font_Sizer.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Demo_Font_Sizer.py b/Demo_Font_Sizer.py index 355e2d84..40878f2f 100644 --- a/Demo_Font_Sizer.py +++ b/Demo_Font_Sizer.py @@ -8,18 +8,14 @@ form = sg.FlexForm("Font size selector") fontSize = 12 -layout = [ - [sg.Text("Aa", size=(2, 1), font="Helvetica " + str(fontSize), key='text'), - sg.Spin([sz for sz in range(6, 72)], font=('Helvetica 20'), initial_value=fontSize, change_submits=True, - key='spin'), sg.Slider(range=(6,50), orientation='h', size=(10,20), change_submits=True, key='slider')], - [sg.OK(), sg.Cancel()] - ] +layout = [[sg.Spin([sz for sz in range(6, 172)], font=('Helvetica 20'), initial_value=fontSize, change_submits=True, key='spin'), + sg.Slider(range=(6,172), orientation='h', size=(10,20), change_submits=True, key='slider', font=('Helvetica 20')), sg.Text("Aa", size=(2, 1), font="Helvetica " + str(fontSize), key='text')]] sz = fontSize form.Layout(layout) while True: button, values= form.Read() - if button in (None, 'OK', 'Cancel'): + if button is None: break sz_spin = int(values['spin']) sz_slider = int(values['slider']) From ab926279166b9cf6587b4075334223ac46e15a6f Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 4 Sep 2018 12:55:25 -0400 Subject: [PATCH 2/4] Check into Dev Branch from Master --- Demo_DOC_Viewer_PIL.py | 238 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 Demo_DOC_Viewer_PIL.py diff --git a/Demo_DOC_Viewer_PIL.py b/Demo_DOC_Viewer_PIL.py new file mode 100644 index 00000000..ac2c8232 --- /dev/null +++ b/Demo_DOC_Viewer_PIL.py @@ -0,0 +1,238 @@ +""" +@created: 2018-08-19 18:00:00 +@author: (c) 2018 Jorj X. McKie +Display a PyMuPDF Document using Tkinter +------------------------------------------------------------------------------- +Dependencies: +------------- +PyMuPDF, PySimpleGUI (requires Python 3), Tkinter, PIL +License: +-------- +GNU GPL V3+ +Description +------------ +Get filename and start displaying page 1. Please note that all file types +of MuPDF are supported (including EPUB e-books and HTML files for example). +Pages can be directly jumped to, or buttons can be used for paging. + +This version contains enhancements: +* Use of PIL improves response times by a factor 3 or more. +* Zooming is now flexible: only one button serves as a toggle. Arrow keys can + be used for moving the window when zooming. + +We also interpret keyboard events (PageDown / PageUp) and mouse wheel actions +to support paging as if a button was clicked. Similarly, we do not include +a 'Quit' button. Instead, the ESCAPE key can be used, or cancelling the form. +To improve paging performance, we are not directly creating pixmaps from +pages, but instead from the fitz.DisplayList of the page. A display list +will be stored in a list and looked up by page number. This way, zooming +pixmaps and page re-visits will re-use a once-created display list. + +""" +import sys +import fitz +import PySimpleGUI as sg +import tkinter as tk +from PIL import Image, ImageTk +import time + +if len(sys.argv) == 1: + rc, fname = sg.GetFileBox('Document Browser', 'Document file to open', + file_types = ( + ("PDF Files", "*.pdf"), + ("XPS Files", "*.*xps"), + ("Epub Files", "*.epub"), + ("Fiction Books", "*.fb2"), + ("Comic Books", "*.cbz"), + ("HTML", "*.htm*"), + # add more document types here + ) + ) +else: + fname = sys.argv[1] + +if not fname: + sg.MsgBox("Cancelling:", "No filename supplied") + raise SystemExit("Cancelled: no filename supplied") + +doc = fitz.open(fname) +page_count = len(doc) + +# used for response time statistics only +fitz_img_time = 0.0 +tk_img_time = 0.0 +img_count = 1 + +# allocate storage for page display lists +dlist_tab = [None] * page_count + +title = "PyMuPDF display of '%s', pages: %i" % (fname, page_count) + +def get_page(pno, zoom = False, max_size = None, first = False): + """Return a PNG image for a document page number. + """ + dlist = dlist_tab[pno] # get display list of page number + if not dlist: # create if not yet there + dlist_tab[pno] = doc[pno].getDisplayList() + dlist = dlist_tab[pno] + r = dlist.rect # the page rectangle + clip = r + # ensure image fits screen: + # exploit, but do not exceed width or height + zoom_0 = 1 + if max_size: + zoom_0 = min(1, max_size[0] / r.width, max_size[1] / r.height) + if zoom_0 == 1: + zoom_0 = min(max_size[0] / r.width, max_size[1] / r.height) + mat_0 = fitz.Matrix(zoom_0, zoom_0) + + if not zoom: # show total page + pix = dlist.getPixmap(matrix = mat_0, alpha=False) + else: + mp = r.tl + (r.br - r.tl) * 0.5 # page rect center + w2 = r.width / 2 + h2 = r.height / 2 + clip = r * 0.5 + tl = zoom[0] # old top-left + tl.x += zoom[1] * (w2 / 2) + tl.x = max(0, tl.x) + tl.x = min(w2, tl.x) + tl.y += zoom[2] * (h2 / 2) + tl.y = max(0, tl.y) + tl.y = min(h2, tl.y) + clip = fitz.Rect(tl, tl.x + w2, tl.y + h2) + + mat = mat_0 * fitz.Matrix(2, 2) # zoom matrix + pix = dlist.getPixmap(alpha=False, matrix=mat, clip=clip) + + if first: # first call: tkinter still inactive + img = pix.getPNGData() # so use fitz png output + else: # else take tk photo image + pilimg = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) + img = ImageTk.PhotoImage(pilimg) + + return img, clip.tl # return image, clip position + + +root = tk.Tk() +max_width = root.winfo_screenwidth() - 20 +max_height = root.winfo_screenheight() - 135 +max_size = (max_width, max_height) +root.destroy() +del root + +form = sg.FlexForm(title, return_keyboard_events = True, + location = (0,0), use_default_focus = False) + +cur_page = 0 +data, clip_pos = get_page(cur_page, + zoom = False, + max_size = max_size, + first = True) + +image_elem = sg.Image(data = data) + +goto = sg.InputText(str(cur_page + 1), size=(5, 1), do_not_clear=True, + key = "PageNumber") + +layout = [ + [ + sg.ReadFormButton('Next'), + sg.ReadFormButton('Prev'), + sg.Text('Page:'), + goto, + sg.Text('(%i)' % page_count), + sg.ReadFormButton('Zoom'), + sg.Text('(toggle on/off, use arrows to navigate while zooming)'), + ], + [image_elem], +] + +form.Layout(layout) + +# now define the buttons / events we want to handle +enter_buttons = [chr(13), "Return:13"] +quit_buttons = ["Escape:27", chr(27)] +next_buttons = ["Next", "Next:34", "MouseWheel:Down"] +prev_buttons = ["Prev", "Prior:33", "MouseWheel:Up"] +Up = "Up:38" +Left = "Left:37" +Right = "Right:39" +Down = "Down:40" +zoom_buttons = ["Zoom", Up, Down, Left, Right] + +# all the buttons we will handle +my_keys = enter_buttons + next_buttons + prev_buttons + zoom_buttons + +# old page store and zoom toggle +old_page = 0 +old_zoom = False + +while True: + button, value = form.Read() + if button is None and (value is None or value['PageNumber'] is None): + break + if button in quit_buttons: + break + + zoom_pressed = False + zoom = False + + if button in enter_buttons: + try: + cur_page = int(value['PageNumber']) - 1 # check if valid + while cur_page < 0: + cur_page += page_count + except: + cur_page = 0 # this guy's trying to fool me + + elif button in next_buttons: + cur_page += 1 + elif button in prev_buttons: + cur_page -= 1 + elif button == Up: + zoom = (clip_pos, 0, -1) + elif button == Down: + zoom = (clip_pos, 0, 1) + elif button == Left: + zoom = (clip_pos, -1, 0) + elif button == Right: + zoom = (clip_pos, 1, 0) + elif button == "Zoom": + zoom_pressed = True + zoom = (clip_pos, 0, 0) + + # sanitize page number + if cur_page >= page_count: # wrap around + cur_page = 0 + while cur_page < 0: # pages > 0 look nicer + cur_page += page_count + + if zoom_pressed and old_zoom: + zoom = zoom_pressed = old_zoom = False + + t0 = time.perf_counter() + data, clip_pos = get_page(cur_page, zoom = zoom, max_size = max_size, + first = False) + t1 = time.perf_counter() + image_elem.Update(data = data) + t2 = time.perf_counter() + fitz_img_time += t1 - t0 + tk_img_time += t2 - t1 + img_count += 1 + old_page = cur_page + old_zoom = zoom_pressed or zoom + + # update page number field + if button in my_keys: + goto.Update(str(cur_page + 1)) + + +# print some response time statistics +if img_count > 0: + print("response times for '%s'" % doc.name) + print("%.4f" % (fitz_img_time/img_count), "sec fitz avg. image time") + print("%.4f" % (tk_img_time/img_count), "sec tk avg. image time") + print("%.4f" % ((fitz_img_time + tk_img_time)/img_count), "sec avg. total time") + print(img_count, "images read") + print(page_count, "pages") From 16b1c8310d0263d641d7f6c815040e55f618414d Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 4 Sep 2018 13:40:31 -0400 Subject: [PATCH 3/4] New options for PopupGetFile (save_as, no_window), added key to buttons, New Demo_Fill_form from JFong --- Demo_Fill_Form.py | 83 ++++++++++++++++++++++++++++++----------------- PySimpleGUI.py | 81 ++++++++++++++++++++++++++------------------- 2 files changed, 100 insertions(+), 64 deletions(-) diff --git a/Demo_Fill_Form.py b/Demo_Fill_Form.py index 1b8e24e6..c638de53 100644 --- a/Demo_Fill_Form.py +++ b/Demo_Fill_Form.py @@ -1,52 +1,75 @@ +# from tkinter.filedialog import asksaveasfilename, askopenfilename +import pickle import PySimpleGUI as sg -""" -Show a complex form, save the initial values -When the "Fill" button is clicked, the form will reset to the initial values -""" + +def save(values): + sfilename = sg.PopupGetFile('Save Settings', save_as=True, no_window=True) + if not sfilename: + return + with open(sfilename, 'wb') as sf: + pickle.dump(values, sf) + + +def load(form): + dfilename = sg.PopupGetFile('Load Settings', no_window=True) + if not dfilename: + return + with open(dfilename, 'rb') as df: + form.Fill(pickle.load(df)) + def Everything(): - sg.ChangeLookAndFeel('Dark') + sg.ChangeLookAndFeel('TanBlue') - - column1 = [[sg.Text('Column 1', background_color='black', justification='center', size=(10, 1))], - [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 1')], - [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 2')], - [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 3')]] + column1 = [ + [sg.Text('Column 1', background_color=sg.DEFAULT_BACKGROUND_COLOR, justification='center', size=(10, 1))], + [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 1', key='spin1')], + [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 2', key='spin2')], + [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 3', key='spin3')]] layout = [ [sg.Text('All graphic widgets in one form!', size=(30, 1), font=("Helvetica", 25))], [sg.Text('Here is some text.... and a place to enter text')], - [sg.InputText('This is my text')], - [sg.Checkbox('Checkbox'), sg.Checkbox('My second checkbox!', default=True)], - [sg.Radio('My first Radio! ', "RADIO1", default=True), sg.Radio('My second Radio!', "RADIO1")], - [sg.Multiline(default_text='This is the default Text should you decide not to type anything', size=(35, 3)), - sg.Multiline(default_text='A second multi-line', size=(35, 3))], - [sg.InputCombo(('Combobox 1', 'Combobox 2'), size=(20, 1)), - sg.Slider(range=(1, 100), orientation='h', size=(34, 20), default_value=85)], - [sg.InputOptionMenu(('Menu Option 1', 'Menu Option 2', 'Menu Option 3'))], - [sg.Listbox(values=('Listbox 1', 'Listbox 2', 'Listbox 3'), size=(30, 3)), - sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=25), - sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=75), - sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=10, key='slider'), - sg.Column(column1, background_color='black')], + [sg.InputText('This is my text', key='in1', do_not_clear=True)], + [sg.Checkbox('Checkbox', key='cb1'), sg.Checkbox('My second checkbox!', key='cb2', default=True)], + [sg.Radio('My first Radio! ', "RADIO1", key='rad1', default=True), + sg.Radio('My second Radio!', "RADIO1", key='rad2')], + [sg.Multiline(default_text='This is the default Text should you decide not to type anything', size=(35, 3), + key='multi1', do_not_clear=True), + sg.Multiline(default_text='A second multi-line', size=(35, 3), key='multi2', do_not_clear=True)], + [sg.InputCombo(('Combobox 1', 'Combobox 2'), key='combo', size=(20, 1)), + sg.Slider(range=(1, 100), orientation='h', size=(34, 20), key='slide1', default_value=85)], + [sg.InputOptionMenu(('Menu Option 1', 'Menu Option 2', 'Menu Option 3'), key='optionmenu')], + [sg.Listbox(values=('Listbox 1', 'Listbox 2', 'Listbox 3'), size=(30, 3), key='listbox'), + sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=25, key='slide2', ), + sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=75, key='slide3', ), + sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=10, key='slide4'), + sg.Column(column1, background_color='gray34')], [sg.Text('_' * 80)], [sg.Text('Choose A Folder', size=(35, 1))], [sg.Text('Your Folder', size=(15, 1), auto_size_text=False, justification='right'), - sg.InputText('Default Folder'), sg.FolderBrowse()], - [sg.ReadFormButton('Fill'), sg.Cancel()] + sg.InputText('Default Folder', key='folder', do_not_clear=True), sg.FolderBrowse()], + [sg.ReadFormButton('Exit'), + sg.Text(' ' * 40), sg.ReadFormButton('SaveSettings'), sg.ReadFormButton('LoadSettings')] ] form = sg.FlexForm('Form Fill Demonstration', default_element_size=(40, 1)) + # button, values = form.LayoutAndRead(layout, non_blocking=True) form.Layout(layout) - button, initial_values = form.ReadNonBlocking() while True: button, values = form.Read() - if button in (None, 'Cancel'): + + if button is 'SaveSettings': + save(values) + elif button is 'LoadSettings': + load(form) + elif button in ['Exit', None]: break - form.Fill(initial_values) # fill form with whatever initial values were - sg.Popup('Title', 'The results of the form.', 'The button clicked was "{}"'.format(button), 'The values are', values) + # form.CloseNonBlockingForm() -Everything() + +if __name__ == '__main__': + Everything() diff --git a/PySimpleGUI.py b/PySimpleGUI.py index c0a0b4ed..ab33c1e8 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -1510,64 +1510,64 @@ T = Text # return Text(display_text, scale=scale, size=size, auto_size_text=auto_size_text, font=font, text_color=text_color, justification=justification) # ------------------------- FOLDER BROWSE Element lazy function ------------------------- # -def FolderBrowse(target=(ThisRow, -1), button_text='Browse', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, pad=None): - return Button(BUTTON_TYPE_BROWSE_FOLDER, target=target, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, pad=pad) +def FolderBrowse(target=(ThisRow, -1), button_text='Browse', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, pad=None, key=None): + return Button(BUTTON_TYPE_BROWSE_FOLDER, target=target, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, pad=pad, key=key) # ------------------------- FILE BROWSE Element lazy function ------------------------- # -def FileBrowse(target=(ThisRow, -1), file_types=(("ALL Files", "*.*"),), button_text='Browse', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, pad=None): - return Button(BUTTON_TYPE_BROWSE_FILE, target, button_text=button_text, file_types=file_types, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, pad=pad) +def FileBrowse(target=(ThisRow, -1), file_types=(("ALL Files", "*.*"),), button_text='Browse', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, pad=None, key=None): + return Button(BUTTON_TYPE_BROWSE_FILE, target, button_text=button_text, file_types=file_types, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, pad=pad, key=key) # ------------------------- FILES BROWSE Element (Multiple file selection) lazy function ------------------------- # -def FilesBrowse(target=(ThisRow, -1), file_types=(("ALL Files", "*.*"),), button_text='Browse', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, pad=None): - return Button(BUTTON_TYPE_BROWSE_FILES, target, button_text=button_text, file_types=file_types, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, pad=pad) +def FilesBrowse(target=(ThisRow, -1), file_types=(("ALL Files", "*.*"),), button_text='Browse', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, pad=None, key=None): + return Button(BUTTON_TYPE_BROWSE_FILES, target, button_text=button_text, file_types=file_types, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, pad=pad, key=key) # ------------------------- FILE BROWSE Element lazy function ------------------------- # -def FileSaveAs(target=(ThisRow, -1), file_types=(("ALL Files", "*.*"),), button_text='Save As...', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, pad=None): - return Button(BUTTON_TYPE_SAVEAS_FILE, target, button_text=button_text, file_types=file_types, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, pad=pad) +def FileSaveAs(target=(ThisRow, -1), file_types=(("ALL Files", "*.*"),), button_text='Save As...', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, pad=None, key=None): + return Button(BUTTON_TYPE_SAVEAS_FILE, target, button_text=button_text, file_types=file_types, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, pad=pad, key=key) # ------------------------- SAVE AS Element lazy function ------------------------- # -def SaveAs(target=(ThisRow, -1), file_types=(("ALL Files", "*.*"),), button_text='Save As...', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, pad=None): - return Button(BUTTON_TYPE_SAVEAS_FILE, target, button_text=button_text, file_types=file_types, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, pad=pad) +def SaveAs(target=(ThisRow, -1), file_types=(("ALL Files", "*.*"),), button_text='Save As...', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, pad=None, key=None): + return Button(BUTTON_TYPE_SAVEAS_FILE, target, button_text=button_text, file_types=file_types, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, pad=pad, key=key) # ------------------------- SAVE BUTTON Element lazy function ------------------------- # -def Save(button_text='Save', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True,font=None, focus=False, pad=None): - return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color,font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) +def Save(button_text='Save', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True,font=None, focus=False, pad=None, key=None): + return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color,font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key) # ------------------------- SUBMIT BUTTON Element lazy function ------------------------- # -def Submit(button_text='Submit', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True,font=None, focus=False, pad=None): - return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color,font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) +def Submit(button_text='Submit', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True,font=None, focus=False, pad=None, key=None): + return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color,font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key) # ------------------------- OPEN BUTTON Element lazy function ------------------------- # -def Open(button_text='Open', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True,font=None, focus=False, pad=None): - return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color,font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) +def Open(button_text='Open', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True,font=None, focus=False, pad=None, key=None): + return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color,font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key) # ------------------------- OK BUTTON Element lazy function ------------------------- # -def OK(button_text='OK', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True, font=None,focus=False, pad=None): - return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color,font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) +def OK(button_text='OK', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True, font=None,focus=False, pad=None, key=None): + return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color,font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key) # ------------------------- YES BUTTON Element lazy function ------------------------- # -def Ok(button_text='Ok', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True, font=None,focus=False, pad=None): - return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) +def Ok(button_text='Ok', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True, font=None,focus=False, pad=None, key=None): + return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key) # ------------------------- CANCEL BUTTON Element lazy function ------------------------- # -def Cancel(button_text='Cancel', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None): - return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) +def Cancel(button_text='Cancel', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None, key=None): + return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key) # ------------------------- QUIT BUTTON Element lazy function ------------------------- # -def Quit(button_text='Quit', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None): - return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) +def Quit(button_text='Quit', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None, key=None): + return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key) # ------------------------- Exit BUTTON Element lazy function ------------------------- # -def Exit(button_text='Exit', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None): - return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) +def Exit(button_text='Exit', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None, key=None): + return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key) # ------------------------- YES BUTTON Element lazy function ------------------------- # -def Yes(button_text='Yes', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None,font=None, bind_return_key=True, focus=False, pad=None): - return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) +def Yes(button_text='Yes', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None,font=None, bind_return_key=True, focus=False, pad=None, key=None): + return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key) # ------------------------- NO BUTTON Element lazy function ------------------------- # -def No(button_text='No', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None,font=None, bind_return_key=False, focus=False, pad=None): - return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) +def No(button_text='No', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None,font=None, bind_return_key=False, focus=False, pad=None, key=None): + return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key) # ------------------------- GENERIC BUTTON Element lazy function ------------------------- # # this is the only button that REQUIRES button text field @@ -2024,7 +2024,6 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form): element.TKListbox.insert(tk.END, item) if element.DefaultValues is not None and item in element.DefaultValues: element.TKListbox.selection_set(index) - # element.TKListbox.selection_set(0,0) if element.BackgroundColor is not None and element.BackgroundColor != COLOR_SYSTEM_DEFAULT: element.TKListbox.configure(background=element.BackgroundColor) if text_color is not None and text_color != COLOR_SYSTEM_DEFAULT: @@ -2036,6 +2035,7 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form): element.TKListbox.pack(side=tk.LEFT) vsb.pack(side=tk.LEFT, fill='y') listbox_frame.pack(side=tk.LEFT,padx=element.Pad[0], pady=element.Pad[1]) + element.TKListbox.bind('', element.ReturnKeyHandler) # ------------------------- INPUT MULTI LINE element ------------------------- # elif element_type == ELEM_TYPE_INPUT_MULTILINE: default_text = element.DefaultText @@ -2183,7 +2183,8 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form): tkscale.config(highlightthickness=0) if element.BackgroundColor is not None and element.BackgroundColor != COLOR_SYSTEM_DEFAULT: tkscale.configure(background=element.BackgroundColor) - tkscale.config(troughcolor=DEFAULT_SCROLLBAR_COLOR) + if DEFAULT_SCROLLBAR_COLOR != COLOR_SYSTEM_DEFAULT: + tkscale.config(troughcolor=DEFAULT_SCROLLBAR_COLOR) 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]) @@ -2948,10 +2949,21 @@ GetFile = GetFileBox AskForFile = GetFileBox -def PopupGetFile(message, default_path='', file_types=(("ALL Files", "*.*"),), button_color=None, size=(None, None)): +def PopupGetFile(message, save_as=False, no_window=False, default_path='', file_types=(("ALL Files", "*.*"),), button_color=None, size=(None, None)): + if no_window: + if save_as: + return tk.filedialog.asksaveasfilename(filetypes=file_types) # show the 'get file' dialog box + else: + return tk.filedialog.askopenfilename(filetypes=file_types) # show the 'get file' dialog box + + if save_as: + browse_button = SaveAs(file_types=file_types) + else: + browse_button = FileBrowse(file_types=file_types) + with FlexForm(title=message, auto_size_text=True, button_color=button_color) as form: layout = [[Text(message, auto_size_text=True)], - [InputText(default_text=default_path, size=size), FileBrowse(file_types=file_types)], + [InputText(default_text=default_path, size=size), browse_button], [Ok(), Cancel()]] (button, input_values) = form.LayoutAndRead(layout) @@ -2961,6 +2973,7 @@ def PopupGetFile(message, default_path='', file_types=(("ALL Files", "*.*"),), b path = input_values[0] return path + # ============================== GetTextBox =========# # Get a single line of text # # ===================================================# From 4697d051bf6ea8ec3e16a9d54be7545bb7952172 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 4 Sep 2018 14:07:42 -0400 Subject: [PATCH 4/4] changed select_submits to change_submits --- Demo_Img_Viewer.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Demo_Img_Viewer.py b/Demo_Img_Viewer.py index ba243fab..df075f69 100644 --- a/Demo_Img_Viewer.py +++ b/Demo_Img_Viewer.py @@ -71,7 +71,7 @@ file_num_display_elem = sg.Text('File 1 of {}'.format(num_files), size=(15,1)) col = [[filename_display_elem], [image_elem]] -col_files = [[sg.Listbox(values = fnames, select_submits=True, size=(60,30), key='listbox')], +col_files = [[sg.Listbox(values = fnames, change_submits=True, size=(60, 30), key='listbox')], [sg.ReadFormButton('Next', size=(8,2)), sg.ReadFormButton('Prev', size=(8,2)), file_num_display_elem]] @@ -111,3 +111,5 @@ while True: filename_display_elem.Update(filename) # update page display file_num_display_elem.Update('File {} of {}'.format(i+1, num_files)) + +