Форум сайта python.su
Есть ли возможность проверить undo/redo стеки виджета Text, пустые они или нет, без изменения самого текста. У меня получается только так:
import tkinter as tk class CustomText(tk.Text): def __init__(self, parent=None): tk.Text.__init__(self, parent, undo=True, wrap='word') self.pack(expand=1, fill='both') self.cmenu = tk.Menu(self, tearoff=False) self.cmenu.add_command(label='Undo', command=self.undo, accelerator='Ctrl+Z') self.cmenu.add_command(label='Redo', command=self.redo, accelerator='Ctrl+Y') self.bind('<ButtonPress-3>', self.right_but_bind) def right_but_bind(self, event): #Проверка изменялся ли текст #Для Undo if self.edit_modified(): self.cmenu.entryconfig(0, state='normal') else: self.cmenu.entryconfig(0, state='disabled') #Для Redo try: self.edit_redo() except: print('Стек redo пуст') self.cmenu.entryconfig(1, state='disabled') else: self.edit_undo() self.cmenu.entryconfig(1, state='normal') pos_x = self.winfo_rootx() + event.x pos_y = self.winfo_rooty() + event.y self.cmenu.tk_popup(pos_x, pos_y) #Отменить действие def undo(self): self.edit_undo() #Вернуть действие def redo(self): self.edit_redo() if __name__ == '__main__': root = tk.Tk() root.title('Custom Text') frm = tk.Frame(root) CustomText(frm) frm.pack() root.mainloop()
Офлайн