在这段代码中,当用户输入链接时,会显示链接的短版本,但用户无法从GUI复制链接。我该怎么解决这个问题?
import pyshorteners as pr
from tkinter import *
root = Tk()
e = Entry(root, width=50)
e.pack()
def click():
link = e.get()
shortener = pr.Shortener()
Short_Link = shortener.tinyurl.short(link)
Label1 = Label(root, text=Short_Link)
Label1.pack()
Button1 = Button(root, text="Enter link:", command=click)
Button1.pack()
root.mainloop()
使用CTRL+C不能直接从Tkinter Label小部件复制文本。
这是一个简单的Tkinter应用程序,可以将标签的文本复制到剪贴板:
from tkinter import *
from tkinter.messagebox import showinfo
class CopyLabel(Tk):
def __init__(self, text: str):
super(CopyLabel, self).__init__()
self.title('Copy this Label')
self.label_text = text
self.label = Label(self, text=text)
self.label.pack(pady=10, padx=40)
self.copy_button = Button(self, text='COPY TO CLIPBOARD', command=self.copy)
self.copy_button.pack(pady=5, padx=40)
def copy(self):
self.clipboard_clear()
self.clipboard_append(self.label_text)
self.update()
showinfo(parent=self, message='Copied to clipboad!')
if __name__ == "__main__":
app = CopyLabel('Copy me!')
app.mainloop()
在你的代码中自动复制Short_Link你可以做:
import pyshorteners as pr
from tkinter import *
root = Tk()
e = Entry(root, width=50)
e.pack()
def click(master: Tk):
link = e.get()
shortener = pr.Shortener()
Short_Link = shortener.tinyurl.short(link)
master.clipboard_clear()
master.clipboard_append(Short_Link)
master.update()
Label1 = Label(root, text=Short_Link)
Label1.pack()
Button1 = Button(root, text="Enter link:", command=lambda: click(root))
Button1.pack()
root.mainloop()