python - 选择、复制和粘贴表单 Tkinter Python 布局网格
问题描述
我需要选择、复制和粘贴我的表单 tkinter 的一些信息。但我做不到。我想复制名字“安吉丽娜朱莉”并粘贴到任何地方。我该怎么做?我需要使用网格布局来做到这一点。我发现了一些带有包装和放置的教程。但我只对 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, 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', font='Arial 12 bold', foreground="blue",
background="light blue").grid(column=1, row=1, padx=20, 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, 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', font='Arial 12 bold', foreground="blue",
background="light blue")
self.name_label.grid(column=1, row=1, padx=20, pady=10)
self.copy_name_label = ttk.Label(f, width=40, text='', font='Arial 12 bold', foreground="blue",
background="light blue")
self.copy_name_label.grid(column=2, row=1, padx=20, pady=10)
copy_button = tk.Button(f, text="Copy", command=self.copy_name)
copy_button.grid(column=3, row=1, padx=20, 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()
推荐阅读
- c++ - Find start of loop of a link list - Floyds cycle detection algorithm
- android - Getting "Problems reading data from Binary store" when running a Flutter App
- android - Unresolved Kotlin references after updating Android Studio to 2020.3.1 Canary 14 Build
- r - Error warning while using the getSymbols function in R
- excel - 从 VBA 发出 2 封不同的电子邮件
- ios - Vimeo Iframe 在 Ipad 中 15 秒后停止
- node.js - 无法通过 nodejs express 后端加载 create-react-app
- android - 如何在颤动上读取BLE和应用程序之间的字符串
- reactjs - 通过 NextJS 中的 useEffect 监听路由器变化
- objective-c - 如何拦截发送到可编辑 NSTextField 的所有关键事件