2016-05-22 6 views
1

でポップアップメニューを取得しようとすると、
得続ける「NameErrorを:グローバル名 『イベントこのPythonプログラミングに 新しい』が定義されていない」ので、任意のヘルプは素晴らしいことです。私はかなり長いスクリプトとしていくつかのコードを削除しました、私は重要なビットが含まれていると思います。はTkinterの割り当てのためのテキストエディタを設計

from tkinter import * 
from tkinter.ttk import * 
from tkinter.scrolledtext import ScrolledText 
import tkinter.filedialog 
from datetime import datetime, date 
import tkinter.messagebox as box 
import tkinter.font 

class Notepad(Frame): 
      # initialization. receives the master widget 
    def __init__(self, parent): 
     Frame.__init__(self, parent) 

     self.parent = parent 
     self.parent.attributes('-topmost', False) 
     self.GUI() 


    def GUI(self): 
      # Main window and title 
     self.parent.title("Assisgnments") 
     self.pack(fill=BOTH, expand=True) 

     self.columnconfigure(1, weight=1) 
     self.columnconfigure(3, pad=7) 
     self.rowconfigure(3,weight=1) 
     self.rowconfigure(5, pad=7) 
      # Menu bar and drop down ADD 
     menu = Menu(self.parent) 
     self.parent.config(menu=menu) 


     edit = Menu(self.parent, tearoff=0) 
     edit.add_command(label="Image", command=self.showImg) 
     edit.add_command(label="Text", command=lambda:self.showTxt(self)) # 
     menu.add_cascade(label="Edit", menu=edit) 

     self.popup_menu = Menu(self.parent, tearoff=0) 
     self.popup_menu.add_command(label="link") 


      # Text area and buttons created, positioned and labeled 
     self.label = Label(self, text="Notes", font="Bold") 
     self.label.grid(sticky=W, pady=4, padx=5) 

     self.text = ScrolledText(self) 
     self.text.grid(row=1, column=0, columnspan=2, rowspan=6, padx=5, sticky=W+S+N) 



     cbtn = Button(self, text="Save", command=lambda:files.save_text(self)) 
     cbtn.grid(row=10, column=4, pady=4) 

     hbtn = Button(self, text="Open",command=lambda:files.load_file(self), width=10) 
     hbtn.grid(row=10, column=0, padx=5) 

     self.obtn = Button(self, text="Quit", underline=0, command=self.parent.destroy) 
     self.obtn.grid(row=10, column=5, pady=(2,4)) 

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

    def do_popup(self): 
     self.popup_menu.post(event.x_root, event.y_root) 



    def sequence(self,*functions): 
     def func(*args, **kwargs): 
      return_value = None 
      for function in functions: 
       return_value = function(*args, **kwargs) 
      return return_value 
     return func 


class files(Notepad): 
    def load_file(self): 
     self.file_name = tkinter.filedialog.askopenfilename()    #("ProgNotes.txt"),("WebAppNotes.txt") 
     infile = open(self.file_name,"r") 
     self.text.insert(END, infile.read()) 
     infile.close() 

    def note_text(self, name): 
     self.file = open("%sNotes.txt"%name, "r") 
     self.text.delete("1.0",END) 
     self.text.insert(END, self.file.read()) 

    def save_text(self): 

     self.file_name = tkinter.filedialog.asksaveasfilename() 
     outfile = open("%s"%self.file_name, "w") 
     outfile.write(self.text.get("1.0", END)) 
     #outfile = self.text.get(1.0, END) 

    def save_notes(self, name): 
     with open("%sNotes.txt"%name, "w") as self.file: 
      self.file.write(self.text.get("1.0", END)) 
      self.file.close() 

     # New window instance with assignment info 



def main(): 

    root = Tk() 
    file_name = None 
    root.geometry("725x600+300+300") 
    Notepad(root) 
    root.mainloop() 

if __name__ == '__main__': 
    main() 

答えて

0

あなたの問題を解決するには、変更する必要があります:

def do_popup(self): 
    self.popup_menu.post(event.x_root, event.y_root) 

へ:

def do_popup(self, event): 
    self.popup_menu.post(event.x_root, event.y_root) 

AND:

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

へ:

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

デモ

そう、あなたはこのGUIを取得します:

enter image description here

+1

お礼を非常に。それはすべての学習曲線であり、私は遠く離れていない何かである。 –

+1

これが問題を解決するので、回答を受け入れたものとしてマークしてください –

関連する問題