Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_replace_hypertext.py
- from tkinter import *
- root = Tk()
- root.title('Tk_replace_hypertext')
- canvas = Canvas(root, width=400, height=100)
- canvas.pack()
- sample_list = 'this is a sample list of words for the pop up menu'.split()
- hypertext_word = 'click this text to open the menu'
- # insert sample_list into a menu
- menu = Menu(root, tearoff=0)
- for word in sample_list:
- menu.add_command(label=word)
- # create a hypertext
- hypertext = canvas.create_text(200, 50, text=hypertext_word,
- font=('Arial', 20),
- fill='red',
- activefill='green',
- tags='hypertext')
- # bind the hypertext to the menu
- canvas.tag_bind(hypertext, '<Button-1>',
- lambda e: menu.post(e.x_root + 20, e.y_root - 50))
- def replace_hypertext(event):
- # replace the clicked on hypertext word
- # with the menu word clicked on
- try:
- menu_word = menu.entrycget(menu.index('active'), 'label')
- canvas.itemconfigure(hypertext, text=menu_word)
- except:
- 0
- # bind the menu to the hypertext click
- menu.bind('<<MenuSelect>>', replace_hypertext)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement