首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >tkinter记事本程序试图使右击复制粘贴选项真的很新,这不能让它工作

tkinter记事本程序试图使右击复制粘贴选项真的很新,这不能让它工作
EN

Stack Overflow用户
提问于 2019-08-29 07:05:01
回答 2查看 711关注 0票数 0

我有一个基本的记事本程序,你可以保存东西,但我想当你右击后突出显示的东西,我希望能够复制/粘贴。

我没怎么试过,不知道该怎么做。

Python代码(记事本)

代码语言:javascript
运行
复制
import tkinter 
import os     
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *

class Notepad: 
    __root = Tk()

# default window width and height 
__thisWidth = 300
__thisHeight = 300
__thisTextArea = Text(__root) 
__thisMenuBar = Menu(__root) 
__thisFileMenu = Menu(__thisMenuBar, tearoff=0) 
__thisEditMenu = Menu(__thisMenuBar, tearoff=0) 
__thisHelpMenu = Menu(__thisMenuBar, tearoff=0) 

# To add scrollbar 
__thisScrollBar = Scrollbar(__thisTextArea)      
__file = None

def __init__(self,**kwargs): 

    # Set icon 
    try: 
            self.__root.wm_iconbitmap("Notepad.ico")  
    except: 
            pass

    # Set window size (the default is 300x300) 

    try: 
        self.__thisWidth = kwargs['width'] 
    except KeyError: 
        pass

    try: 
        self.__thisHeight = kwargs['height'] 
    except KeyError: 
        pass

    # Set the window text 
    self.__root.title("Untitled - Notepad") 

    # Center the window 
    screenWidth = self.__root.winfo_screenwidth() 
    screenHeight = self.__root.winfo_screenheight() 

    # For left-align 
    left = (screenWidth / 2) - (self.__thisWidth / 2)  

    # For right-align 
    top = (screenHeight / 2) - (self.__thisHeight /2)  

    # For top and bottom 
    self.__root.geometry('%dx%d+%d+%d' % (self.__thisWidth, 
                                          self.__thisHeight, 
                                          left, top))  

    # To make the textarea auto resizable 
    self.__root.grid_rowconfigure(0, weight=1) 
    self.__root.grid_columnconfigure(0, weight=1) 

    # Add controls (widget) 
    self.__thisTextArea.grid(sticky = N + E + S + W) 

    # To open new file 
    self.__thisFileMenu.add_command(label="New", 
                                    command=self.__newFile)     

    # To open a already existing file 
    self.__thisFileMenu.add_command(label="Open", 
                                    command=self.__openFile) 

    # To save current file 
    self.__thisFileMenu.add_command(label="Save", 
                                    command=self.__saveFile)     

    # To create a line in the dialog         
    self.__thisFileMenu.add_separator()                                          
    self.__thisFileMenu.add_command(label="Exit", 
                                    command=self.__quitApplication) 
    self.__thisMenuBar.add_cascade(label="File", 
                                   menu=self.__thisFileMenu)      

    # To give a feature of cut  
    self.__thisEditMenu.add_command(label="Cut", 
                                    command=self.__cut)              

    # to give a feature of copy     
    self.__thisEditMenu.add_command(label="Copy", 
                                    command=self.__copy)          

    # To give a feature of paste 
    self.__thisEditMenu.add_command(label="Paste", 
                                    command=self.__paste)          

    # To give a feature of editing 
    self.__thisMenuBar.add_cascade(label="Edit", 
                                   menu=self.__thisEditMenu)      

    # To create a feature of description of the notepad 
    self.__thisHelpMenu.add_command(label="About Notepad", 
                                    command=self.__showAbout)  
    self.__thisMenuBar.add_cascade(label="Help", 
                                   menu=self.__thisHelpMenu) 

    self.__root.config(menu=self.__thisMenuBar) 

    self.__thisScrollBar.pack(side=RIGHT,fill=Y)                     

    # Scrollbar will adjust automatically according to the content         
    self.__thisScrollBar.config(command=self.__thisTextArea.yview)      
    self.__thisTextArea.config(yscrollcommand=self.__thisScrollBar.set) 


def __quitApplication(self): 
    self.__root.destroy() 
    # exit() 

def __showAbout(self): 
    showinfo("Notepad","Mrinal Verma") 

def __openFile(self): 

    self.__file = askopenfilename(defaultextension=".txt", 
                                  filetypes=[("All Files","*.*"), 
                                    ("Text Documents","*.txt")]) 

    if self.__file == "": 

        # no file to open 
        self.__file = None
    else: 

        # Try to open the file 
        # set the window title 
        self.__root.title(os.path.basename(self.__file) + " - Notepad") 
        self.__thisTextArea.delete(1.0,END) 

        file = open(self.__file,"r") 

        self.__thisTextArea.insert(1.0,file.read()) 

        file.close() 







def __newFile(self): 
    self.__root.title("Untitled - Notepad") 
    self.__file = None
    self.__thisTextArea.delete(1.0,END) 

def __saveFile(self): 

    if self.__file == None: 
        # Save as new file 
        self.__file = asksaveasfilename(initialfile='Untitled.txt', 
                                        defaultextension=".txt", 
                                        filetypes=[("All Files","*.*"), 
                                            ("Text Documents","*.txt")]) 

        if self.__file == "": 
            self.__file = None
        else: 

            # Try to save the file 
            file = open(self.__file,"w") 
            file.write(self.__thisTextArea.get(1.0,END)) 
            file.close() 

            # Change the window title 
            self.__root.title(os.path.basename(self.__file) + " - Notepad") 


    else: 
        file = open(self.__file,"w") 
        file.write(self.__thisTextArea.get(1.0,END)) 
        file.close() 

def __cut(self): 
    self.__thisTextArea.event_generate("<<Cut>>") 

def __copy(self): 
    self.__thisTextArea.event_generate("<<Copy>>") 

def __paste(self): 
    self.__thisTextArea.event_generate("<<Paste>>") 

def run(self): 

    # Run main application 
    self.__root.mainloop() 







# Run main application 
notepad = Notepad(width=600,height=400) 
notepad.run() 

我认为可行的方法

代码语言:javascript
运行
复制
w = Label(root, text="Right-click to display menu", width=40, height=20)
w.pack()

# create a menu
popup = Menu(root, tearoff=0)
popup.add_command(label="Next") # , command=next) etc...
popup.add_command(label="Previous")
popup.add_separator()
popup.add_command(label="Home")

def do_popup(event):
# display the popup menu
try:
    popup.tk_popup(event.x_root, event.y_root, 0)
finally:
    # make sure to release the grab (Tk 8.0a1 only)
    popup.grab_release()

w.bind("<Button-3>", do_popup)

b = Button(root, text="Quit", command=root.destroy)
 b.pack()

我期望当你在突出显示后右击时,你可以复制粘贴。目前没有用于复制/粘贴的右键菜单或功能。如果可以放在右击弹出窗口中,您可以删除复制粘贴顶部的选项卡。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-29 14:13:22

易建利给出了正确的答案。

右击记事本的完整版本如下所示。我自由地重构了程序,使其与PEP8更兼容,主要是关于变量名,不使用通配符导入(*),并删除了一些注释,因为代码是自我描述的。

代码语言:javascript
运行
复制
import os
from tkinter import Tk, Text, Menu, Scrollbar, N, S, E, W, RIGHT, Y, END
from tkinter.messagebox import showinfo
from tkinter.filedialog import asksaveasfilename, askopenfilename


class RightClicker:
    def __init__(self, event):
        right_click_menu = Menu(None, tearoff=0, takefocus=0)

        for txt in ['Cut', 'Copy', 'Paste']:
            right_click_menu.add_command(
                label=txt, command=lambda event=event, text=txt:
                self.right_click_command(event, text))

        right_click_menu.tk_popup(event.x_root + 40, event.y_root + 10, entry='0')

    def right_click_command(self, event, cmd):
        event.widget.event_generate(f'<<{cmd}>>')


class Notepad:

    def __init__(self, **kwargs):
        self.file_name = None
        self.root = Tk()
        self.root.title("Untitled - Notepad")
        self.text_area = Text(self.root)

        menu_bar = Menu(self.root)
        file_menu = Menu(menu_bar, tearoff=0)
        edit_menu = Menu(menu_bar, tearoff=0)
        help_menu = Menu(menu_bar, tearoff=0)
        self.root.config(menu=menu_bar)

        scrollbar = Scrollbar(self.text_area)
        scrollbar.pack(side=RIGHT, fill=Y)
        scrollbar.config(command=self.text_area.yview)
        self.text_area.config(yscrollcommand=scrollbar.set)

        self.text_area.bind('<Button-3>', RightClicker)

        # set icon and window size (default is 300 x 300)
        try:
            self.root.wm_iconbitmap("Notepad.ico")
        except:   #pylint: disable=W0702
            pass

        try:
            width = kwargs['width']
        except KeyError:
            width = 300

        try:
            height = kwargs['height']
        except KeyError:
            height = 300

        # place notepad in the center of the screen
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()
        left = (screen_width / 2) - (width / 2)
        top = (screen_height / 2) - (height / 2)
        self.root.geometry('%dx%d+%d+%d' % (width, height, left, top))

        self.root.grid_rowconfigure(0, weight=1)
        self.root.grid_columnconfigure(0, weight=1)

        self.text_area.grid(sticky=N + E + S + W)

        # file menu controls
        file_menu.add_command(label="New", command=self.new_file)
        file_menu.add_command(label="Open", command=self.open_file)
        file_menu.add_command(label="Save", command=self.save_file)
        file_menu.add_separator()
        file_menu.add_command(label="Exit", command=self.quit_application)
        menu_bar.add_cascade(label="File", menu=file_menu)

        # edit menu controls
        edit_menu.add_command(label="Cut", command=self.cut_text)
        edit_menu.add_command(label="Copy", command=self.copy_text)
        edit_menu.add_command(label="Paste", command=self.paste_text)
        menu_bar.add_cascade(label="Edit", menu=edit_menu)

        # help menu controls
        help_menu.add_command(label="About Notepad", command=self.show_about)
        menu_bar.add_cascade(label="Help", menu=help_menu)

    def quit_application(self):
        self.root.destroy()

    def show_about(self):
        showinfo("Notepad", "Mrinal Verma")

    def open_file(self):
        self.file_name = askopenfilename(
            defaultextension=".txt",
            filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])

        if self.file_name == "":
            self.file_name = None

        else:
            self.root.title(os.path.basename(self.file_name) + " - Notepad")
            self.text_area.delete(1.0, END)

            with open(self.file_name, 'r') as file:
                self.text_area.insert(1.0, file.read())

    def new_file(self):
        self.root.title("Untitled - Notepad")
        self.file_name = None
        self.text_area.delete(1.0, END)

    def save_file(self):
        if self.file_name is None:
            self.file_name = asksaveasfilename(
                initialfile='Untitled.txt', defaultextension=".txt",
                filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])

            if self.file_name == "":
                self.file_name = None

            else:
                with open(self.file_name, 'w') as file:
                    file.write(self.text_area.get(1.0, END))

                self.root.title(os.path.basename(self.file_name) + " - Notepad")

        else:
            with open(self.file_name, 'w') as file:
                file.write(self.text_area.get(1.0, END))

    def cut_text(self):
        self.text_area.event_generate("<<Cut>>")

    def copy_text(self):
        self.text_area.event_generate("<<Copy>>")

    def paste_text(self):
        self.text_area.event_generate("<<Paste>>")

    def run_notepad(self):
        self.root.mainloop()


def main():
    notepad = Notepad(width=600, height=400)
    notepad.run_notepad()


if __name__ == '__main__':
    main()
票数 4
EN

Stack Overflow用户

发布于 2019-08-29 12:16:56

您没有将<Button-3>绑定到您的Text小部件。

只需将以下代码添加到您的类构造函数中:

代码语言:javascript
运行
复制
def __init__(self,**kwargs):
    ...
    self.__thisTextArea.bind("<Button-3>",lambda e: self.__thisMenuBar.tk_popup(e.x_root + 40, e.y_root + 10, entry="0"))

但通常我建议做一个rightclicker类,并将其应用于您想要的任何小部件,如下所示:

代码语言:javascript
运行
复制
import tkinter as tk

root = tk.Tk()

class RightClicker:
    def __init__(self, e):
        commands = ["Cut","Copy","Paste"]
        menu = tk.Menu(None, tearoff=0, takefocus=0)

        for txt in commands:
            menu.add_command(label=txt, command=lambda e=e,txt=txt:self.click_command(e,txt))

        menu.tk_popup(e.x_root + 40, e.y_root + 10, entry="0")

    def click_command(self, e, cmd):
        e.widget.event_generate(f'<<{cmd}>>')

text = tk.Text(root)
text.pack()
text.bind("<Button-3>", RightClicker)
entry = tk.Entry(root)
entry.pack()
entry.bind("<Button-3>", RightClicker)
root.mainloop()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57701023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档