Reputation: 57
Part of a project I'm working on requires I copy a generated "password" variable to the clipboard. I'm using PySimpleGUI and when the password is generated, I click a "Copy" button and it should save the password to the clipboard, however, trying to paste this to an external program (Google Chrome for example), nothing happens, but if I press Ctrl+V in a box on the PySimpleGUI interface, the password appears and I can then paste it to an external program (Google Chrome).
if event == "Copy" and password is not None:
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(password)
Is there a way I can make it so the password can be pasted straight from clipboard to google without having to paste it inside the program first?
Upvotes: 0
Views: 418
Reputation: 13051
There's no code provided by you for the GUI.
Following code work well.
import win32clipboard
import PySimpleGUI as sg
layout = [
[sg.Input(key='INPUT')],
[sg.Button('COPY')],
]
window = sg.Window('Title', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'COPY':
text = values['INPUT']
if text:
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(text)
win32clipboard.CloseClipboard()
window.close()
Upvotes: 0
Reputation: 57
One additional line of code needed for this to function properly.
win32clipboard.CloseClipboard()
I'm not sure why it would allow me to paste in google if I had first pasted it inside the GUI, so if anyone knows the answer then please say, but the line of code above will solve similar issues.
Upvotes: 0