Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_replace_label_text.py
- from tkinter import *
- root = Tk()
- root.title('Tk_replace_label_text')
- 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'
- menu = Menu(root, tearoff=0)
- for word in sample_list:
- menu.add_command(label=word)
- text_label = Label(root, text='')
- text_label.place(x=0, y=10, width=400)
- def show_menu(event):
- menu.post(text_label.winfo_rootx()+300, int(menu.winfo_height()))
- def target_text(selected):
- global text_2_change
- text_2_change = Label(text_label, text=selected, fg='blue', cursor='hand2')
- text_2_change.place(x=10)
- text_2_change.bind('<Button-1>', show_menu)
- target_text(hypertext_word)
- def menucallback(event): # hacked solution
- index = root.call(event.widget, "index","active")
- selected = menu.entrycget(index, 'label')
- if selected:
- try:
- text_2_change.destroy()
- except:
- 0
- target_text(selected)
- text_label.bind('<Button-1>', show_menu)
- menu.bind('<<MenuSelect>>', menucallback)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement