首页 > 解决方案 > 如何使用python创建复制和粘贴程序

问题描述

如何使用 Python 编写可以在 Windows 中充当复制和粘贴的程序?我用python库做了两个按钮,想在windows环境中复制一个,另一个在粘贴中。谢谢你帮助我。

这是我的代码

import tkinter as tk
import pyautogui as pg
import time


root = tk.Tk()
root.geometry("200x160")
root.resizable(False, False)


is_checked = tk.IntVar()


def alont():
    if is_checked.get() == 0:
        root.attributes('-topmost', False)   
    else:
        root.attributes('-topmost', True)

text1 = tk.Entry(root, font = ("area", 10), width = 5)

text1.config(insertwidth=1)

text1.place(x=10, y=137)

text1.insert(0, 1)
timer1 = text1.get()

def copy1():

    timer1 = text1.get()
    tm = int(timer1)
    time.sleep(tm)
    pg.hotkey('ctrl', 'c')

def paste1():

    timer1 = text1.get()
    tm = int(timer1)
    time.sleep(tm)
    pg.hotkey('ctrl', 'v')



btn1 = tk.Button(root, text="Copy", font=("area", 25), height = 1, width = 10)
btn1.config(command=copy1)
btn1.pack()

btn2 = tk.Button(root, text="Paste", font=("area", 25), height = 1, width = 10)
btn2.config(command=paste1)
btn2.pack()

chbutton = tk.Checkbutton(root, text="Always On Top", 
font=("area", 12), variable=is_checked)
chbutton.config(command=alont)
chbutton.place(x=60, y=133.5)


root.mainloop()

标签: python

解决方案


有这方面的现有库。看到这个答案。还有pyperclip。


推荐阅读