如何检查用户在使用剪贴板时右键单击的输入框?

时间:2017-07-27 17:58:54

标签: python tkinter

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

而是将所有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()

2 个答案:

答案 0 :(得分:2)

为了获得您正在寻找的行为,我已经重新设计了您的代码以实现您想要的目标。

1你不应该像你在这里一样两次导入tkinter。只需使用import tkinter as tk,这对于tkinter所需的一切都可以正常工作。

我们需要确保使用event.widget.focus()方法中的popup将焦点放在您右键点击的小部件上,并将当前焦点置于帧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()

答案 1 :(得分:0)

传递给回调的event对象有一个widget属性,告诉您哪个小部件收到了该事件。

您还可以通过常用窗口小部件方法focus_get向tkinter询​​问当前具有焦点的窗口小部件。