问题描述
我需要选择,复制和粘贴tkinter表单的某些信息。但是我做不到。我想复制名称“ Angelina Jolie”并粘贴到任何地方。我该怎么办?我需要使用“网格布局”来做到这一点。我找到了一些关于打包和放置的教程。但是我只对Lyaout Grid感兴趣
from tkinter import *
from tkinter import ttk
import tkinter as tk
class Form:
def __init__(self):
pass
def form(self):
self.root = Tk()
self.root.geometry('850x600')
self.root.title("PVE - Formulário Estudar")
self.canvas = tk.Canvas(bg='light blue',scrollregion=(0,1500,3300))
self.canvas.bind('<Enter>',self._bound_to_mousewheel)
self.canvas.bind('<Leave>',self._unbound_to_mousewheel)
self.canvas.pack(fill='both',expand=True)
f = tk.Frame(self.canvas,background="light blue")
# you need to create a window into the canvas for the widget to scroll
self.canvas.create_window((5,5),window=f,anchor="nw")
yvbar = ttk.Scrollbar(self.canvas,orient='vertical',command=self.canvas.yview)
xvbar = ttk.Scrollbar(self.canvas,orient='horizontal',command=self.canvas.xview)
yvbar.pack(side='right',fill='y')
yvbar.config(command=self.canvas.yview)
xvbar.pack(side='bottom',fill='x')
xvbar.config(command=self.canvas.xview)
self.canvas.config(yscrollcommand=yvbar.set,xscrollcommand=xvbar.set)
ttk.Label(f,width=20,text='Name: ',font='Arial 12 bold',background="light blue",anchor='w')\
.grid(column=0,row=1,padx=20,pady=10)
ttk.Label(f,width=40,text='Angelina Jolie',foreground="blue",background="light blue").grid(column=1,pady=10)
self.root.mainloop()
def _bound_to_mousewheel(self,event):
self.canvas.bind_all("<MouseWheel>",self._on_mousewheel)
def _unbound_to_mousewheel(self,event):
self.canvas.unbind_all("<MouseWheel>")
def _on_mousewheel(self,event):
self.canvas.yview_scroll(int(-1 * (event.delta / 120)),"units")
if __name__ == '__main__':
a = Form()
a.form()
解决方法
我不确定是要将值复制到剪贴板还是要问如何将另一个元素的值设置为现有元素的值,但是我添加了一个复制按钮,可以同时执行这两个操作,看看下面的代码是如何做到的。
from tkinter import *
from tkinter import ttk
import tkinter as tk
import pyperclip
class Form:
def __init__(self):
pass
def copy_name(self):
# Get the current label value
user_name = self.name_label['text']
# Copy the value to the clipboard
pyperclip.copy(user_name)
# Update "paste" the value to the second label
self.copy_name_label.config(text=user_name)
print("{} was copied to your clipboard".format(user_name))
def form(self):
self.root = Tk()
self.root.geometry('850x600')
self.root.title("PVE - Formulário Estudar")
self.canvas = tk.Canvas(bg='light blue',scrollregion=(0,1500,3300))
self.canvas.bind('<Enter>',self._bound_to_mousewheel)
self.canvas.bind('<Leave>',self._unbound_to_mousewheel)
self.canvas.pack(fill='both',expand=True)
f = tk.Frame(self.canvas,background="light blue")
# you need to create a window into the canvas for the widget to scroll
self.canvas.create_window((5,5),window=f,anchor="nw")
yvbar = ttk.Scrollbar(self.canvas,orient='vertical',command=self.canvas.yview)
xvbar = ttk.Scrollbar(self.canvas,orient='horizontal',command=self.canvas.xview)
yvbar.pack(side='right',fill='y')
yvbar.config(command=self.canvas.yview)
xvbar.pack(side='bottom',fill='x')
xvbar.config(command=self.canvas.xview)
self.canvas.config(yscrollcommand=yvbar.set,xscrollcommand=xvbar.set)
ttk.Label(f,width=20,text='Name: ',font='Arial 12 bold',background="light blue",anchor='w') \
.grid(column=0,row=1,padx=20,pady=10)
self.name_label = ttk.Label(f,width=40,text='Angelina Jolie',foreground="blue",background="light blue")
self.name_label.grid(column=1,pady=10)
self.copy_name_label = ttk.Label(f,text='',background="light blue")
self.copy_name_label.grid(column=2,pady=10)
copy_button = tk.Button(f,text="Copy",command=self.copy_name)
copy_button.grid(column=3,pady=10)
self.root.mainloop()
def _bound_to_mousewheel(self,event):
self.canvas.bind_all("<MouseWheel>",self._on_mousewheel)
def _unbound_to_mousewheel(self,event):
self.canvas.unbind_all("<MouseWheel>")
def _on_mousewheel(self,event):
self.canvas.yview_scroll(int(-1 * (event.delta / 120)),"units")
if __name__ == '__main__':
a = Form()
a.form()
,
我终于在本教程中找到了解决我问题的方法: http://www.booneputney.com/development/tkinter-copy-to-clipboard/
from tkinter import *
from tkinter import ttk
import tkinter as tk
class Form:
name = "Angelina Jolie"
def __init__(self):
pass
def copy_text_to_clipboard(self,event):
self.name = event.widget.get("1.0",'end-1c') # get field value from event,but remove line return at end
self.root.clipboard_clear() # clear clipboard contents
self.root.clipboard_append(self.name) # append new value to clipbaord
def form(self):
self.root = Tk()
self.root.geometry('850x600')
self.root.title("PVE - Formulário Estudar")
self.canvas = tk.Canvas(bg='light blue',anchor='w')\
.grid(column=0,pady=10)
text_name = Text(f,height=1,borderwidth=0)
text_name.insert(1.0,self.name)
text_name.grid(row=1,column=1)
# Bind left click on text widget to copy_text_to_clipboard() function
text_name.bind("<Button-1>",self.copy_text_to_clipboard)
self.root.mainloop()
def _bound_to_mousewheel(self,"units")
if __name__ == '__main__':
a = Form()
a.form()