Advertisement
here2share

# Tk_replace_hypertext.py

Aug 4th, 2022 (edited)
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. # Tk_replace_hypertext.py
  2.  
  3. from tkinter import *
  4.  
  5. root = Tk()
  6. root.title('Tk_replace_hypertext')
  7.  
  8. canvas = Canvas(root, width=400, height=100)
  9. canvas.pack()
  10.  
  11. sample_list = 'this is a sample list of words for the pop up menu'.split()
  12.  
  13. hypertext_word = 'click this text to open the menu'
  14.  
  15. # insert sample_list into a menu
  16. menu = Menu(root, tearoff=0)
  17. for word in sample_list:
  18.     menu.add_command(label=word)
  19.  
  20. # create a hypertext
  21. hypertext = canvas.create_text(200, 50, text=hypertext_word,
  22.                                font=('Arial', 20),
  23.                                fill='red',
  24.                                activefill='green',
  25.                                tags='hypertext')
  26.  
  27. # bind the hypertext to the menu
  28. canvas.tag_bind(hypertext, '<Button-1>',
  29.                 lambda e: menu.post(e.x_root + 20, e.y_root - 50))
  30.  
  31. def replace_hypertext(event):
  32.     # replace the clicked on hypertext word
  33.     # with the menu word clicked on
  34.     try:
  35.         menu_word = menu.entrycget(menu.index('active'), 'label')
  36.         canvas.itemconfigure(hypertext, text=menu_word)
  37.     except:
  38.         0
  39.  
  40. # bind the menu to the hypertext click
  41. menu.bind('<<MenuSelect>>', replace_hypertext)
  42.  
  43. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement