tkinter - I have too fix something in my Copy&Paste program in Python -
this copy&paste program:
from tkinter import * import pmw class copytextwindow(frame): def __init__(self): frame.__init__(self) pmw.initialise() self.pack(expand=yes, fill=both) self.master.title("scrolledtext demo") self.frame1=frame(self, bg="white") self.frame1.pack(expand=yes, fill=both) self.text1=pmw.scrolledtext(self, text_width=25, text_height=12, text_wrap=word, hscrollmode="static", vscrollmode="static") self.text1.pack(side=left, expand=yes, fill=both, padx=5, pady=5) options = ["copy", "paste"] self.selectedoption = stringvar() self.menu = menu(self.frame1, tearoff=0) option in options: self.menu.add_radiobutton( label=option, variable=self.selectedoption, command=self.executeoption) self.text1.bind("<button-3>", self.popupmenu) def popupmenu(self, event): self.menu.post(event.x_root, event.y_root) def executeoption(self): if self.selectedoption.get()=="copy": self.copiedtext=self.text1.get(sel_first, sel_last) else: self.text1.settext( self.text1.get()+self.copiedtext) def main(): copytextwindow().mainloop() if __name__=="__main__": main()
in program, want make gui, in can copy , paste text have selected. when press right mouse button, little menu copy , paste options.
the program opens up, when press right mouse button, no menu appears. python doesn't complain error.
i need understand mistake in code.
for reason ignore, event doesn't seem triggered when bind on text or on frame, works when it's on main window:
from tkinter import * import pmw class copytextwindow(frame): def __init__(self, master=none): # i've added master option pass frame frame.__init__(self, master) pmw.initialise() self.pack(expand=yes, fill=both) self.master.title("scrolledtext demo") self.frame1=frame(self, bg="white") self.frame1.pack(expand=yes, fill=both) self.text1=pmw.scrolledtext(self, text_width=25, text_height=12, text_wrap=word, hscrollmode="static", vscrollmode="static") self.text1.pack(side=left, expand=yes, fill=both, padx=5, pady=5) options = ["copy", "paste"] self.selectedoption = stringvar() self.menu = menu(self.frame1, tearoff=0) option in options: self.menu.add_radiobutton( label=option, variable=self.selectedoption, command=self.executeoption) def popupmenu(self, event): print("ok") self.menu.post(event.x_root, event.y_root) def executeoption(self): if self.selectedoption.get()=="copy": self.copiedtext=self.text1.get(sel_first, sel_last) else: self.text1.settext( self.text1.get()+self.copiedtext) def main(): # main window root = tk() ctw = copytextwindow(root) # bind on main window root.bind("<button-3>", ctw.popupmenu) root.mainloop() if __name__=="__main__": main()
Comments
Post a Comment