简体   繁体   English

选择、复制和粘贴表单 Tkinter Python 布局网格

[英]Select, copy and paste form Tkinter Python Layout Grid

I need to select, to copy and to paste some information of my form tkinter.我需要选择、复制和粘贴我的表单 tkinter 的一些信息。 But I cant do it.但我做不到。 I want to copy the name 'Angelina Jolie' and paste anywhere.我想复制“安吉丽娜·朱莉”这个名字并粘贴到任何地方。 How I can do it?我该怎么做? I need to do this with Grid Layout.我需要使用网格布局来做到这一点。 I found some tutorials with pack and place.我找到了一些带包装和放置的教程。 But I'm only interested in the Lyaout Grid但我只对 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()

I'm not sure if you want to copy the value to the clipboard or you meant to ask how to set the value of another element to the value of an existing element, however, I added a copy button that does both, take a look at the code below for how I did it.我不确定您是要将值复制到剪贴板还是想询问如何将另一个元素的值设置为现有元素的值,但是,我添加了一个可以同时执行这两个操作的复制按钮,请看在下面的代码中,我是如何做到的。

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()

I finally find a solution for my problem in this tutorial: http://www.booneputney.com/development/tkinter-copy-to-clipboard/我终于在本教程中找到了解决我的问题的方法: 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', 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)

        text_name = Text(f, width=40, height=1, font='Arial 12 bold', background="light blue", foreground="blue",
                     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, 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM