Moved clipboard operations to a single function to make event loop cleaner
This commit is contained in:
parent
f2f0ea99a7
commit
6e7d267583
|
@ -25,6 +25,29 @@ import PySimpleGUI as sg
|
|||
right_click_menu = ['', ['Copy', 'Paste', 'Select All', 'Cut']]
|
||||
MLINE_KEY = '-MLINE-'
|
||||
|
||||
def do_clipboard_operation(event, window, element):
|
||||
if event == 'Select All':
|
||||
element.Widget.selection_clear()
|
||||
element.Widget.tag_add('sel', '1.0', 'end')
|
||||
elif event == 'Copy':
|
||||
try:
|
||||
text = element.Widget.selection_get()
|
||||
window.TKroot.clipboard_clear()
|
||||
window.TKroot.clipboard_append(text)
|
||||
except:
|
||||
print('Nothing selected')
|
||||
elif event == 'Paste':
|
||||
element.Widget.insert(sg.tk.INSERT, window.TKroot.clipboard_get())
|
||||
elif event == 'Cut':
|
||||
try:
|
||||
text = element.Widget.selection_get()
|
||||
window.TKroot.clipboard_clear()
|
||||
window.TKroot.clipboard_append(text)
|
||||
element.update('')
|
||||
except:
|
||||
print('Nothing selected')
|
||||
|
||||
def main():
|
||||
layout = [ [sg.Text('Using a custom right click menu with Multiline Element')],
|
||||
[sg.Multiline(size=(60,20), key=MLINE_KEY, right_click_menu=right_click_menu)],
|
||||
[sg.B('Go'), sg.B('Exit')]]
|
||||
|
@ -35,28 +58,14 @@ mline:sg.Multiline = window[MLINE_KEY]
|
|||
|
||||
while True:
|
||||
event, values = window.read() # type: (str, dict)
|
||||
print(event, values)
|
||||
if event in (sg.WIN_CLOSED, 'Exit'):
|
||||
break
|
||||
if event == 'Select All':
|
||||
mline.Widget.selection_clear()
|
||||
mline.Widget.tag_add('sel', '1.0', 'end')
|
||||
elif event == 'Copy':
|
||||
try:
|
||||
text = mline.Widget.selection_get()
|
||||
window.TKroot.clipboard_clear()
|
||||
window.TKroot.clipboard_append(text)
|
||||
except:
|
||||
print('Nothing selected')
|
||||
elif event == 'Paste':
|
||||
mline.Widget.insert(sg.tk.INSERT, window.TKroot.clipboard_get())
|
||||
elif event == 'Cut':
|
||||
try:
|
||||
text = mline.Widget.selection_get()
|
||||
window.TKroot.clipboard_clear()
|
||||
window.TKroot.clipboard_append(text)
|
||||
mline.update('')
|
||||
except:
|
||||
print('Nothing selected')
|
||||
|
||||
# if event is a right click menu for the multiline, then handle the event in func
|
||||
if event in right_click_menu[1]:
|
||||
do_clipboard_operation(event, window, mline)
|
||||
|
||||
window.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue