【问题标题】:Put menu Copy, Paste, Cut, Select All In notepad将菜单复制,粘贴,剪切,全选放在记事本中
【发布时间】:2020-05-22 12:52:53
【问题描述】:

早上好,我正在尝试将复制、粘贴、剪切和全选菜单放在笔记本中,我只设法将其放在文本框中,但没有放在笔记本中。带有选项的 Def 我将它作为 ExtryEx 但我不太清楚如何声明它。我调查了但我没有找到任何例子来指导我,希望你能帮助我谢谢!

PSD:当你点击 Compile Pls 时会弹出记事本,但是它不会打印任何内容,因为这部分代码没有添加,因为它太大了。

from bs4 import BeautifulSoup
import requests
import pandas as pd
import tkinter
from tkinter import *
import contextlib
import io



ventana = tkinter.Tk()
ventana.geometry("600x500")


username_label = Label(text="T cap", bg="#FFEEDD")
username_label.place(x=22, y=70)
password_label = Label(text="Mo cap", bg="#FFEEDD")
password_label.place(x=22, y=130)
fullname_label = Label(text="Lista de Episodio RZ", bg="#FFEEDD")
fullname_label.place(x=22, y=190)
fecha_label = Label(text="Ingrese Fecha Mesa y Año Actual, Ejemplo: 2020/05", bg="#FFEEDD")
fecha_label.place(x=22,y=250)

username = StringVar()
password = StringVar()
fullname = StringVar()
fecha = StringVar()


class EntryEx(tkinter.Entry):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.menu = tkinter.Menu(self, tearoff=False)
        self.menu.add_command(label="Copiar", command=self.popup_copy)
        self.menu.add_command(label="Cortar", command=self.popup_cut)
        self.menu.add_separator()
        self.menu.add_command(label="Pegar", command=self.popup_paste)
        self.menu.add_command(label="Seleccionar Todo", command=self.popup_Selectall)
        self.bind("<Button-3>", self.display_popup)

    def display_popup(self, event):
        self.menu.post(event.x_root, event.y_root)

    def popup_copy(self):
        self.event_generate("<<Copy>>")
    def popup_cut(self):
        self.event_generate("<<Cut>>")
    def popup_paste(self):
        self.event_generate("<<Paste>>")
    def popup_Selectall(self):
        self.event_generate("<<SelectAll>>")    

username_entry = EntryEx(textvariable=username, width="70")
password_entry = EntryEx(textvariable=password, width="70")
fullname_entry = EntryEx(textvariable=fullname, width="70")
fecha_entry = EntryEx(textvariable=fecha, width="70")

username_entry.place(x=22, y=100)
password_entry.place(x=22, y=160)
fullname_entry.place(x=22, y=220)
fecha_entry.place(x=22, y=280)





def embed():
    toplevel = tkinter.Toplevel(ventana)
    toplevel.state("zoomed")
    text = tkinter.Text(toplevel)
    text.pack(fill=tkinter.BOTH, expand=True)

    file = io.StringIO()
    with contextlib.redirect_stdout(file):
        algunos_prints()
        text.insert(tkinter.END, file.getvalue())

boton_btn = tkinter.Button(ventana, text="Compila Pls",command=embed,width="30",height="2",bg="#00CD63")
boton_btn.place(x=22,y=400)

ventana.mainloop()

【问题讨论】:

  • @stovfl def embed():
  • @stovfl 记事本弹出,点击“Compile Pls”打开
  • 您无需做任何不同的事情。您似乎已经知道如何将菜单添加到根窗口,为什么不对Toplevel 做同样的事情呢?目前还不清楚是什么问题。
  • @BryanOakley 我的意思是在博客的任意位置右击会出现一个小菜单,里面有复制粘贴剪切和全选的选项,我也试过把它放在Toplevel菜单但我没有成功,帮助Plis
  • @RicharlyxD 看看我的回答。

标签: python tkinter


【解决方案1】:

我假设您希望在右键单击记事本时弹出右键菜单。

在这里,我修改了你的代码:

#from bs4 import BeautifulSoup
import requests
import pandas as pd
import tkinter
from tkinter import *
import contextlib
import io




ventana = tkinter.Tk()
ventana.geometry("600x500")


username_label = Label(text="T cap", bg="#FFEEDD")
username_label.place(x=22, y=70)
password_label = Label(text="Mo cap", bg="#FFEEDD")
password_label.place(x=22, y=130)
fullname_label = Label(text="Lista de Episodio RZ", bg="#FFEEDD")
fullname_label.place(x=22, y=190)
fecha_label = Label(text="Ingrese Fecha Mesa y Año Actual, Ejemplo: 2020/05", bg="#FFEEDD")
fecha_label.place(x=22,y=250)

username = StringVar()
password = StringVar()
fullname = StringVar()
fecha = StringVar()


class EntryEx(tkinter.Entry):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.menu = tkinter.Menu(self, tearoff=False)
        self.menu.add_command(label="Copiar", command=self.popup_copy)
        self.menu.add_command(label="Cortar", command=self.popup_cut)
        self.menu.add_separator()
        self.menu.add_command(label="Pegar", command=self.popup_paste)
        self.menu.add_command(label="Seleccionar Todo", command=self.popup_Selectall)
        self.bind("<Button-3>", self.display_popup)

    def display_popup(self, event):
        self.menu.post(event.x_root, event.y_root)

    def popup_copy(master):
        master.event_generate("<<Copy>>")
    def popup_cut(master):
        master.event_generate("<<Cut>>")
    def popup_paste(master):
        master.event_generate("<<Paste>>")
    def popup_Selectall(master):
        master.event_generate("<<SelectAll>>")    

username_entry = EntryEx(textvariable=username, width="70")
password_entry = EntryEx(textvariable=password, width="70")
fullname_entry = EntryEx(textvariable=fullname, width="70")
fecha_entry = EntryEx(textvariable=fecha, width="70")

username_entry.place(x=22, y=100)
password_entry.place(x=22, y=160)
fullname_entry.place(x=22, y=220)
fecha_entry.place(x=22, y=280)





def embed():
    toplevel = tkinter.Toplevel(ventana)
    toplevel.state("zoomed")
    text = tkinter.Text(toplevel)
    text.pack(fill=tkinter.BOTH, expand=True)
    def display_popup(event):
        menu2.post(event.x_root, event.y_root)

    def popup_copy():
        EntryEx.popup_copy(text)
    def popup_cut():
        EntryEx.popup_cut(text)
    def popup_paste():
        EntryEx.popup_paste(text)
    def popup_Selectall():
        EntryEx.popup_Selectall(text)
    file = io.StringIO()
    menu2 = tkinter.Menu(toplevel, tearoff=False)
    menu2.add_command(label="Copy", command=popup_copy)
    menu2.add_command(label="Cut", command=popup_cut)
    menu2.add_separator()
    menu2.add_command(label="Paste", command=popup_paste)
    menu2.add_command(label="Select All", command=popup_Selectall)
    text.bind("<Button-3>", display_popup)
    """
    with contextlib.redirect_stdout(file):
        algunos_prints()
        text.insert(tkinter.END, file.getvalue())
    """
boton_btn = tkinter.Button(ventana, text="Compila Pls",command=embed,width="30",height="2",bg="#00CD63")
boton_btn.place(x=22,y=400)

ventana.mainloop()

我注释掉了“algunos_prints()”的东西,因为我假设它出现在你的代码后面,而你没有包含它。

输出: [

希望这会有所帮助!

【讨论】:

  • 这很完美!,但是在写东西的时候它并没有实现它的功能:/
  • @RicharlyxD 我有一个问题。 event_generate 函数是什么?它来自什么模块?
  • @TheMaket 它非常适合我,非常感谢!!
  • @RicharlyxD 如果它有效,你介意支持并接受它吗?这样,未来的人们就会知道要看哪个答案。谢谢!
  • 谢谢! @RicharlyxD 很多
猜你喜欢
  • 1970-01-01
  • 2010-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 2013-05-13
相关资源
最近更新 更多