Copying from clipboard using tkinter without displaying window
Asked Answered
B

9

11

Running Python 3.4 on Windows 7.

I need to copy what's stored in the clipboard to a variable in my python program. I've seen on Stack Overflow that that can be done either with pywin32 or tkinter. Since tkinter is part of the python standard library, I decided that that was the better of the two since the user won't have to install an external module. Here's the code for getting the clipboard data in tkinter:

import tkinter
number = tkinter.Tk().clipboard_get()

This works fine except a blank tkinter window pops up every time this executes.

  1. Why is this happening? Normally tkinter doesn't display anything until tk().mainloop() is run.

  2. Is there any way to avoid this window popping up? If not, I guess I'll just use pywin32.

Balanchine answered 3/7, 2014 at 17:12 Comment(0)
M
4

Window is created by tkinter.Tk() (or other elements which need window) not by tk().mainloop(). Mainloop keeps program working.

Maybe try Pyperclip or clipboard

Mosher answered 3/7, 2014 at 17:30 Comment(3)
those two modules are great thanks. Do you know why the paste() returns a byte object instead of a string?Balanchine
I use Python 2.7 on Linux Mint and I get string. Probably it depends on system.Mosher
Interesting. Python 3.4 on Windows 7 gives a byte object. I just converted it to utf-8 stringBalanchine
A
21

This works fine except a blank tkinter window pops up every time this executes.

You can hide this window:

from tkinter import Tk
root = Tk()
root.withdraw()
number = root.clipboard_get()
Aloisia answered 12/10, 2014 at 1:43 Comment(0)
M
4

Window is created by tkinter.Tk() (or other elements which need window) not by tk().mainloop(). Mainloop keeps program working.

Maybe try Pyperclip or clipboard

Mosher answered 3/7, 2014 at 17:30 Comment(3)
those two modules are great thanks. Do you know why the paste() returns a byte object instead of a string?Balanchine
I use Python 2.7 on Linux Mint and I get string. Probably it depends on system.Mosher
Interesting. Python 3.4 on Windows 7 gives a byte object. I just converted it to utf-8 stringBalanchine
A
3

Here's a Python function based on this answer that replaces/returns clipboard text using Tkinter, a built-in Python module, without showing the window.

def use_clipboard(paste_text=None):
    import tkinter # For Python 2, replace with "import Tkinter as tkinter".
    tk = tkinter.Tk()
    tk.withdraw()
    if type(paste_text) == str: # Set clipboard text.
        tk.clipboard_clear()
        tk.clipboard_append(paste_text)
    try:
        clipboard_text = tk.clipboard_get()
    except tkinter.TclError:
        clipboard_text = ''
    r.update() # Stops a few errors (clipboard text unchanged, command line program unresponsive, window not destroyed).
    tk.destroy()
    return clipboard_text

A small disadvantage with using this Tkinter based method is that it uses a quickly hidden window which isn't ideal but this shouldn't be noticeable.
This answer uses content from my original answer on the Stack Overflow question How to copy/get an image in the clipboard with Python (I accept Tkinter for text).

Armandinaarmando answered 31/8, 2014 at 17:48 Comment(1)
You don't define r in this answer. Presumably tk.update() is the correct lineShelbyshelden
A
2
AnnoyingWindow = Tk()
ClipBoard = AnnoyingWindow.clipboard_get()
AnnoyingWindow.destroy()
print(ClipBoard)
Aspectual answered 10/6, 2016 at 10:9 Comment(0)
M
1

I had the same problem. This worked for me on windows 7, python 2.7. I now only get one window.

from Tkinter import *
root = Tk()
cliptext = root.clipboard_get()
lab=Label(root, text = cliptext)
lab.pack()
root.mainloop()
Moorish answered 22/2, 2016 at 16:26 Comment(0)
G
0
number.withdraw() #this hides the ui for the object

Just add this command at the beginning when you create your TKinter object and it will hide the UI. See this similar question.

Gomez answered 30/4, 2016 at 1:54 Comment(0)
F
0

You can actually do it without tkinter and in a much more simple way with a non-default module called pyperclip.

Install with:

pip install pyperclip

Usage:

import pyperclip

clipboard_content = pyperclip.paste()
Fruity answered 8/3, 2017 at 15:32 Comment(1)
Good but not a default moduleRochellrochella
S
0

A tkinter way without window:

from tkinter import Tk


def clipboard_get():
    r = Tk()
    r.withdraw()
    return r.clipboard_get()
Synchromesh answered 10/9, 2019 at 13:55 Comment(0)
R
0

Just two simple lines

It works in Windows & Linux (shall work cross platform as explained below)

import PySimpleGUI as sg

# Paste clipboard (get clipboard content and assign to variable data)

data = sg.clipboard_get()


# Copy clipboard (send string from variable text to clipboard)

text = 'My text to clipboard'
sg.clipboard_set(text)

This does not requires you to open/hide any window and it uses Tkinter in fact you may want to take a look to PySimpleGUI which is far easier than Tkinter, has a lot of documentation with so many examples that in minutes you can start having your own GUI design.

It's surprising that Python GUI code is completely cross platform from Windows to Mac to Linux. No source code changes. This is true for both PySimpleGUI and PySimpleGUIQt https://www.pysimplegui.org/en/latest/#tkinter-version

Rhapsodic answered 15/8, 2023 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.