gpt4 book ai didi

python - 使用剪贴板时如何检查用户右键单击了哪个输入框?

转载 作者:行者123 更新时间:2023-12-01 02:44:51 25 4
gpt4 key购买 nike

所以我有一个带有 3 个输入框的表单。您可以右键单击 3 个输入框之一,然后选择“复制”。这应该复制您单击的特定输入框中的文本。

相反,所有 3 个输入框中的文本都会复制到剪贴板。如何仅从用户右键单击的输入框中复制文本?

#! /usr/bin/python
import os
import pypyodbc
import tkinter
from tkinter import ttk
from tkinter import messagebox
from tkinter import BOTH, END, LEFT
import tkinter as tk


class Adder(ttk.Frame):
"""The adders gui and functions."""
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.root = parent
self.init_gui()

# Copy and Paste Buttons BEGIN Here
def button1(self):
self.printcopy()
self.copy()

def copy(self, event=None):
self.clipboard_clear()
text = self.first_entry.get() # This is the method I need to work on.
text2 = self.last_entry.get()
text3 = self.lic_entry.get() # All entries are copied
self.clipboard_append(text)
self.clipboard_append(text2)
self.clipboard_append(text3)

def printcopy(self):
print ('Copy!')

def popup(self, event):
self.aMenu.post(event.x_root, event.y_root)

# Copy and Paste Buttons END Here


def init_gui(self):
"""Builds GUI."""
self.root.title('Verify')
self.root.option_add('*tearOff', 'FALSE')

self.grid(column=0, row=0, sticky='nsew') # this starts the entire form


self.outline = tkinter.LabelFrame(self, height=80, width=900) # border frame
self.outline.grid(column=0, row=1, columnspan=5, padx=5, ipady=10, pady=5)


# Input Boxes and Button

self.first_entry = tkinter.Entry(self, width=28) # first input box
self.first_entry.grid(sticky='', column=1, row=1)

self.last_entry = tkinter.Entry(self, width=28) # second input box
self.last_entry.grid(sticky='', column=2, row=1)

self.lic_entry = tkinter.Entry(self, width=28) # third input box
self.lic_entry.grid(sticky='', column=3, row=1)

# Output frame for answer
self.entries = []
self.output0 = tkinter.Entry(self, width=149, justify='center', bd='0', bg='#E0E0E0')
self.output0.grid(column=0, row=6, columnspan=5, padx=1)
self.entries.append(self.output0)
# Copy and Paste
self.aMenu = tk.Menu(self, tearoff=0)
self.aMenu.add_command(label='Copy', command=self.button1)
self.first_entry.bind("<Button-3>", self.popup)
self.last_entry.bind("<Button-3>", self.popup)
self.lic_entry.bind("<Button-3>", self.popup)
self.output0.bind("<Button-3>", self.popup)

self.blank = tkinter.LabelFrame(self, height=5, bd=0,) # blank line
self.blank.grid(row=16,)

if __name__ == '__main__':
root = tkinter.Tk()
Adder(root)
root.resizable(width=False, height=False) # locks window from being resized
root.mainloop()

最佳答案

为了获得您正在寻找的行为,我重新设计了您的代码来完成您想要的。

第一,您不应该像在这里那样导入 tkinter 两次。只需使用 import tkinter as tk 这就能很好地满足您从 tkinter 需要的所有内容。

我们需要通过在 popup 方法中使用 event.widget.focus() 确保焦点位于您右键单击的小部件上,并获取当前的将焦点放在框架 self.outline 内部,然后 get() 该条目的值。

如果您将 copy() 方法更改为:

def copy(self):
self.clipboard_clear()
self.clipboard_append(self.outline.focus_get().get())

并将您的 popup() 方法更改为:

def popup(self, event):
event.widget.focus()
self.aMenu.post(event.x_root, event.y_root)

它应该按照您的需要工作。

看一下下面的代码:

from tkinter import ttk
import tkinter as tk


class Adder(ttk.Frame):
"""The adders gui and functions."""
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.root = parent
self.root.title('Verify')
self.root.option_add('*tearOff', 'FALSE')
self.grid(column=0, row=0, sticky='nsew')

self.outline = tk.LabelFrame(self, height=80, width=900)
self.outline.grid(column=0, row=1, columnspan=5, padx=5, ipady=10, pady=5)

self.first_entry = tk.Entry(self, width=28)
self.first_entry.grid(sticky='', column=1, row=1)
self.first_entry.bind("<Button-3>", self.popup)

self.last_entry = tk.Entry(self, width=28)
self.last_entry.grid(sticky='', column=2, row=1)
self.last_entry.bind("<Button-3>", self.popup)

self.lic_entry = tk.Entry(self, width=28)
self.lic_entry.grid(sticky='', column=3, row=1)
self.lic_entry.bind("<Button-3>", self.popup)

self.output0 = tk.Entry(self, width=149, justify='center', bd='0', bg='#E0E0E0')
self.output0.grid(column=0, row=6, columnspan=5, padx=1)
self.output0.bind("<Button-3>", self.popup)

self.aMenu = tk.Menu(self, tearoff=0)
self.aMenu.add_command(label='Copy', command=self.copy)

self.blank = tk.LabelFrame(self, height=5, bd=0) # blank line
self.blank.grid(row=16)

def copy(self):
self.clipboard_clear()
self.clipboard_append(self.outline.focus_get().get())


def popup(self, event):
event.widget.focus()# this moves the focus to the entry field you right click in.
self.aMenu.post(event.x_root, event.y_root)


if __name__ == '__main__':
root = tk.Tk()
Adder(root)
root.resizable(width=False, height=False)
root.mainloop()

关于python - 使用剪贴板时如何检查用户右键单击了哪个输入框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45357954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com