Accessing Clipboard in Python version 3.11 with win32clipboard causing failed to open clipboard

this is my first question on stackoverflow, I apologize in case it is an unclear question,
I’m trying to create a productivity software where I manipulate the data I copy to the clipboard and paste the processed version (for now I split the text into rows and paste them individually) but every time I access the clipboard I don’t have a safe method to not conflict with other processes or my program’s processes (this is my assumption)

import win32clipboard
import PySimpleGUI as sg
import threading
import keyboard
import time


def check_clip():
    try:
        win32clipboard.OpenClipboard()
        win32clipboard.CloseClipboard()
        return True
    except Exception as e:
        print(f"Error access clipboard: {e}")
        return False

def run_safe(func):
    if check_clip():
        return func()
    else:
        #time.sleep(0.3)
        return run_safe(func)

def get_clipboard_text():
    win32clipboard.OpenClipboard()
    clipboard_text = win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()
    return clipboard_text


def set_clipboard_text(text):
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardText(text)
    win32clipboard.CloseClipboard()
        

def flush_clipboard():
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.CloseClipboard()


def get_next(column_list):
    run_safe(lambda: set_clipboard_text(column_list[0]))
    keyboard.wait('ctrl+v')
    while column_list:
        item = column_list.pop(0)
        run_safe(lambda: set_clipboard_text(item + "n"))
        keyboard.wait('ctrl+v')
        run_safe(flush_clipboard)
    return True

def script():
    just_flushed = True
    while True:
        if program_running:    
            keyboard.wait('ctrl+c')
            time.sleep(0.3)
            column_text = run_safe(get_clipboard_text)
            column_list = column_text.splitlines()
            get_next(column_list)
            just_flushed = False
        else:
            if not just_flushed:
                run_safe(flush_clipboard)
                just_flushed = True


def main():
    layout = [
        [sg.Text('Controllo del Programma')],
        [sg.Button('Run/Stop', key='-STARTPAUSE-', button_color=('white','red'))],
    ]

    window = sg.Window('MemCP', layout)

    global program_running
    program_running = False
    program_thread = threading.Thread(target=script, daemon=True)
    program_thread.start()
    
    while True:
        event, values = window.read(timeout=100)

        if event == sg.WIN_CLOSED:
            break

        if event == '-STARTPAUSE-':
            program_running = not program_running
            window['-STARTPAUSE-'].update(button_color=('white', 'green' if program_running else 'red'))        

    window.close()


if __name__ == "__main__":
    main()

despite my workarounds every time I access clipboard data I randomly call for the exception to be thrown, is there a consistent method to solve my problem?

I tried to encapsulate all the functions that access it in these exception checking functions

def check_clip():
    try:
        win32clipboard.OpenClipboard()
        win32clipboard.CloseClipboard()
        return True
    except Exception as e:
        print(f"Error access clipboard: {e}")
        return False

def run_safe(func):
    if check_clip():
        return func()
    else:
        #time.sleep(0.3)
        return run_safe(func)

New contributor

Mario Nicolosi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật