Nếu tôi thực thi mã này, nó hoạt động tốt. Nhưng nếu tôi sao chép một cái gì đó bằng bàn phím [Ctrl+C], thì làm thế nào tôi có thể dán văn bản có mặt trên bảng tạm trong bất kỳ hộp nhập hoặc hộp văn bản nào trong Python?
import pyperclip
pyperclip.copy['The text to be copied to the clipboard.']
spam = pyperclip.paste[]
Không gian CDS
2.60917 Huy hiệu vàng33 Huy hiệu bạc36 Huy hiệu đồng17 gold badges33 silver badges36 bronze badges
Đã hỏi ngày 18 tháng 10 năm 2017 lúc 18:56Oct 18, 2017 at 18:56
0
Bạn sẽ muốn vượt qua
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
7 cùng một nơi bạn sẽ đặt một chuỗi cho phần chèn tiện ích nhập hoặc văn bản của bạn.Hãy xem mã ví dụ này.
Có một nút để sao chép những gì trong trường nhập và một để dán vào trường nhập.
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
Ngoài ra, bạn chỉ có thể làm ctrl+v: d
Đã trả lời ngày 18 tháng 10 năm 2017 lúc 19:11Oct 18, 2017 at 19:11
Mike - Smtmike - SMTMike - SMT
15.6K4 Huy hiệu vàng34 Huy hiệu bạc68 Huy hiệu Đồng4 gold badges34 silver badges68 bronze badges
Nếu bạn đã sử dụng
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
8 trong mã của mình và tất cả những gì bạn cần là nội dung trong bảng tạm. Sau đó, import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
8 có một phương pháp được xây dựng để làm điều đó.import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
Để thêm văn bản được sao chép vào mục nhập/văn bản
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
8, bạn có thể sử dụng biến import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
8:var = tk.StringVar[]
var.set[spam]
Và liên kết biến đó với tiện ích nhập.
box = tk.Entry[root, textvariable = var]
Đã trả lời ngày 19 tháng 10 năm 2017 lúc 17:02Oct 19, 2017 at 17:02
RottencandyrottencandyRottenCandy
6381 Huy hiệu vàng13 Huy hiệu bạc22 Huy hiệu đồng1 gold badge13 silver badges22 bronze badges
Bạn cần xóa dòng sau, vì nó ghi đè lên những gì bạn đã sao chép với bàn phím.
pyperclip.copy['The text to be copied to the clipboard.']
Ví dụ, tôi đã sao chép tiêu đề của bạn, và đây là cách tôi dán nó vào vỏ Python:
>>> import pyperclip
>>> pyperclip.paste[]
'How do I paste the copied text from keyboard in python\n\n'
>>>
Đã trả lời ngày 18 tháng 10 năm 2017 lúc 19:03Oct 18, 2017 at 19:03
Trong Python, bạn có thể sao chép văn bản [chuỗi] vào bảng tạm và dán [nhận] văn bản từ bảng tạm với pyperclip. Bạn cũng có thể theo dõi bảng tạm để nhận văn bản khi được cập nhật.
- pyperclip · pypi
- asweigart/pyperclip: Mô-đun Python cho các chức năng clipboard đa nền tảng.
- Chào mừng bạn đến với tài liệu Pyperclip từ! - Tài liệu PYPERCLIP 1.5
import pyperclip
pyperclip.copy['text to be copied']
print[pyperclip.paste[]]
# text to be copied
Bài viết này mô tả các nội dung sau đây.
- Cách cài đặt pyperclip
- Sao chép văn bản vào bảng tạm:
2import tkinter as tk root = tk.Tk[] spam = root.clipboard_get[]
- Dán [Nhận] Văn bản từ bảng tạm:
7import tkinter as tk from tkinter import ttk import pyperclip root = tk.Tk[] some_entry = tk.Entry[root] some_entry.pack[] def update_btn[]: global some_entry pyperclip.copy[some_entry.get[]] def update_btn_2[]: global some_entry # for the insert method the 2nd argument is always the string to be # inserted to the Entry field. some_entry.insert[tk.END, pyperclip.paste[]] btn = ttk.Button[root, text="Copy to clipboard", command = update_btn] btn.pack[] btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2] btn2.pack[] root.mainloop[]
- Giám sát bảng tạm:
4,import tkinter as tk root = tk.Tk[] spam = root.clipboard_get[]
5import tkinter as tk root = tk.Tk[] spam = root.clipboard_get[]
- Lưu ý: PyPerclip chỉ có thể xử lý văn bản [chuỗi]
Pandas cung cấp một chức năng để xử lý nội dung bảng tạm dưới dạng
import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
6.- Pandas: Nhận nội dung Clipboard dưới dạng DataFrame với read_clipboard []
- Pandas: Sao chép dataFrame vào bảng tạm với to_clipboard []
Như đã đề cập trong phần cuối, pyperclip chỉ có thể xử lý văn bản [chuỗi]. Bạn có thể lấy hình ảnh từ bảng tạm với gối.
- Nhận hình ảnh từ bảng tạm với Python, Gối
Các nội dung sau đây được kiểm tra với phiên bản pyperclip
import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
7. Lưu ý rằng nó có thể hoạt động khác nhau trên các phiên bản khác.Cách cài đặt pyperclip
Bạn có thể cài đặt pyperclip với lệnh ________ 28/________ 29.
Đối với Linux, lệnh
var = tk.StringVar[]
var.set[spam]
0 hoặc var = tk.StringVar[]
var.set[spam]
1 [được cài đặt bằng var = tk.StringVar[]
var.set[spam]
2, v.v.] và mô -đun var = tk.StringVar[]
var.set[spam]
3 hoặc var = tk.StringVar[]
var.set[spam]
4 [được cài đặt với import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
8] là bắt buộc. Xem tài liệu chính thức để biết chi tiết.- Chào mừng bạn đến với tài liệu Pyperclip từ! - Tài liệu PYPERCLIP 1.5
Sao chép văn bản vào bảng tạm: import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
2
import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
Bạn có thể sao chép văn bản vào bảng tạm với
import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
2.pyperclip.copy['text to be copied']
Dán [Nhận] Văn bản từ bảng tạm: import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
7
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
Bạn có thể dán văn bản [nhận] từ bảng tạm với
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
7.print[pyperclip.paste[]]
# text to be copied
print[type[pyperclip.paste[]]]
#
Tất nhiên, bạn cũng có thể gán nó cho một biến.
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
0Giám sát bảng tạm: import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
4, import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
5
import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
Bạn có thể theo dõi bảng tạm với
import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
4, box = tk.Entry[root, textvariable = var]
3.Nếu
import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
4 được thực thi khi bảng tạm trống, nó sẽ chờ văn bản mới được sao chép. Khi văn bản mới được sao chép, import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
4 trả về nó.Nếu nó được thực thi với một số văn bản đã được sao chép trên bảng tạm, văn bản sẽ được trả về.
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
1Khi
box = tk.Entry[root, textvariable = var]
3 được thực thi, nó chờ sao chép văn bản mới. Nếu văn bản trên bảng tạm được cập nhật, box = tk.Entry[root, textvariable = var]
3 sẽ trả về nó.import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
2Bạn có thể chỉ định số giây để kiểm tra. Nếu không có văn bản mới nào được sao chép và thời gian được chỉ định trôi qua mà không có giá trị trả về, các chức năng này sẽ tăng
box = tk.Entry[root, textvariable = var]
8.
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
3Ví dụ về xử lý ngoại lệ:
- "Hãy thử ... ngoại trừ ... khác ... cuối cùng ..." trong Python
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
4Lưu ý: PyPerclip chỉ có thể xử lý văn bản [chuỗi]
Pyperclip chỉ có thể xử lý văn bản [chuỗi]. Ngay cả khi bạn sao chép giá trị số với
import tkinter as tk
root = tk.Tk[]
spam = root.clipboard_get[]
2, import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
7 trả về chuỗi pyperclip.copy['The text to be copied to the clipboard.']
1.import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
5Sử dụng
pyperclip.copy['The text to be copied to the clipboard.']
2 hoặc pyperclip.copy['The text to be copied to the clipboard.']
3 để chuyển đổi chuỗi thành số.- Chuyển đổi một chuỗi thành một số [int, float] trong python
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
6Nếu một hình ảnh được sao chép vào bảng tạm,
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk[]
some_entry = tk.Entry[root]
some_entry.pack[]
def update_btn[]:
global some_entry
pyperclip.copy[some_entry.get[]]
def update_btn_2[]:
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert[tk.END, pyperclip.paste[]]
btn = ttk.Button[root, text="Copy to clipboard", command = update_btn]
btn.pack[]
btn2 = ttk.Button[root, text="Paste current clipboard", command = update_btn_2]
btn2.pack[]
root.mainloop[]
7 sẽ trả về một chuỗi trống pyperclip.copy['The text to be copied to the clipboard.']
5. Bạn có thể lấy hình ảnh từ bảng tạm với gối.- Nhận hình ảnh từ bảng tạm với Python, Gối