Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách copy từ clipboard Tkinter không hiển thị cửa sổ

Truy cập clipboard bằng clipboard_get(). Sau khi copy, text nằm trong cache. Tạo cửa sổ lưu text, sau đó ẩn cửa sổ bằng withdraw().

Ví dụ

# Import tkinter
from tkinter import *

win = Tk()
win.geometry("600x200")

# Lấy dữ liệu từ clipboard
cliptext = win.clipboard_get()

Label(win, text=cliptext).pack()

win.mainloop()

Kết quả

Chạy code copy clipboard và hiển thị trong cửa sổ.

Cách copy từ clipboard Tkinter không hiển thị cửa sổ

Để ẩn cửa sổ, dùng withdraw():

from tkinter import *
win = Tk()
win.withdraw()
number = win.clipboard_get()