在不顯示視窗的情況下,透過 tkinter 從剪貼簿中複製內容
假設在一個特定的應用程式中,我們需要複製駐留在剪貼簿中的內容,可以使用 clipboard_get() 訪問剪貼簿。
從剪貼簿複製文字後,它將駐留在快取記憶體中,透過它我們可以除錯程式並在框架中顯示文字,然後我們可以在剪貼簿中看到複製的文字。
首先,我們將建立一個視窗,使用 get 方法來儲存源中複製的字元或文字。一旦執行完成,我們就可以使用 Tkinter 中的“withdraw”方法隱藏視窗。它有助於擺脫視窗。
示例
#Import the tkinter library from tkinter import * #Create an instance of tkinter canvas by executing it win = Tk() win.geometry("600x200") #Get the data from the clipboard cliptext = win.clipboard_get() #Create the label for the clipboard lab=Label(win, text = cliptext) lab.pack() #Keep Running the window win.mainloop()
輸出
執行上面的程式碼片段將複製剪貼簿中的內容並將其顯示在一個視窗中。
為了避免視窗,我們可以使用“withdraw”方法,
from tkinter import * win = Tk() win.withdraw() number = win.clipboard_get()
廣告