添加带有多个小部件链接的右键单击上下文菜单?(Adding a right click context menu with link with multiple widgets?)

编程入门 行业动态 更新时间:2024-10-25 14:24:03
添加带有多个小部件链接的右键单击上下文菜单?(Adding a right click context menu with link with multiple widgets?)

我的问题是对这个问题的一种跟进

我想获得右键单击的小部件对其执行操作。

条件 :

右键点击“我的标签2” 选择“显示标签”

预期成绩 :

=>“我的标签2”应该打印出来

要解决的初始代码:

from Tkinter import * # Tkinter -> tkinter in Python 3 root = Tk() def print_label(): print "Please, help me to print the label which has been clicked !" def popup(event): menu.post(event.x_root, event.y_root) # create a popup menu menu = Menu(root, tearoff=0) menu.add_command(label="Display the label", command=print_label) # create the 3 labels label1_text=StringVar() label2_text=StringVar() label3_text=StringVar() label1_text.set("my label 1") label2_text.set("my label 2") label3_text.set("my label 3") label1=Label(root, textvariable=label1_text) label2=Label(root, textvariable=label2_text) label3=Label(root, textvariable=label3_text) label1.pack() label2.pack() label3.pack() # attach popup to frame label1.bind("<Button-3>", popup) label2.bind("<Button-3>", popup) label3.bind("<Button-3>", popup) root.mainloop()

My issue is a kind of follow up of this question

I would like to get the widget which has been right clicked to do an action on it.

Conditions :

right click on "my label 2" select "Display the label"

Expected results :

=> "my label 2" should be printed

Initial code to solve :

from Tkinter import * # Tkinter -> tkinter in Python 3 root = Tk() def print_label(): print "Please, help me to print the label which has been clicked !" def popup(event): menu.post(event.x_root, event.y_root) # create a popup menu menu = Menu(root, tearoff=0) menu.add_command(label="Display the label", command=print_label) # create the 3 labels label1_text=StringVar() label2_text=StringVar() label3_text=StringVar() label1_text.set("my label 1") label2_text.set("my label 2") label3_text.set("my label 3") label1=Label(root, textvariable=label1_text) label2=Label(root, textvariable=label2_text) label3=Label(root, textvariable=label3_text) label1.pack() label2.pack() label3.pack() # attach popup to frame label1.bind("<Button-3>", popup) label2.bind("<Button-3>", popup) label3.bind("<Button-3>", popup) root.mainloop()

最满意答案

对当前代码进行尽可能少的更改,您需要做三件事:

创建一个全局变量以保存对当前所选标签的引用。

在popup ,将此变量设置为event.widget ,它将是当前选定的标签。

在print_label内部,通过访问其"text"键来打印此标签的文本。

以下是您的程序的固定版本。 我更改的内容在评论框中:

from Tkinter import * # Tkinter -> tkinter in Python 3 root = Tk() ############################################################# selected = None # This is the variable mentioned in step 1 def print_label(): print selected["text"] # This is step 3 def popup(event): global selected # Tell Python that selected is global menu.post(event.x_root, event.y_root) selected = event.widget # This is step 2 ############################################################# # create a popup menu menu = Menu(root, tearoff=0) menu.add_command(label="Display the label", command=print_label) # create the 3 labels label1_text=StringVar() label2_text=StringVar() label3_text=StringVar() label1_text.set("my label 1") label2_text.set("my label 2") label3_text.set("my label 3") label1=Label(root, textvariable=label1_text) label2=Label(root, textvariable=label2_text) label3=Label(root, textvariable=label3_text) label1.pack() label2.pack() label3.pack() # attach popup to frame label1.bind("<Button-3>", popup) label2.bind("<Button-3>", popup) label3.bind("<Button-3>", popup) root.mainloop()

Making as few changes as possible to the current code, you need to do three things:

Create a global variable to hold a reference to the currently selected label.

Inside popup, set this variable to event.widget, which will be the currently selected label.

Inside print_label, print this label's text by accessing its "text" key.

Below is a fixed version of your program. The stuff I changed is in the comment box:

from Tkinter import * # Tkinter -> tkinter in Python 3 root = Tk() ############################################################# selected = None # This is the variable mentioned in step 1 def print_label(): print selected["text"] # This is step 3 def popup(event): global selected # Tell Python that selected is global menu.post(event.x_root, event.y_root) selected = event.widget # This is step 2 ############################################################# # create a popup menu menu = Menu(root, tearoff=0) menu.add_command(label="Display the label", command=print_label) # create the 3 labels label1_text=StringVar() label2_text=StringVar() label3_text=StringVar() label1_text.set("my label 1") label2_text.set("my label 2") label3_text.set("my label 3") label1=Label(root, textvariable=label1_text) label2=Label(root, textvariable=label2_text) label3=Label(root, textvariable=label3_text) label1.pack() label2.pack() label3.pack() # attach popup to frame label1.bind("<Button-3>", popup) label2.bind("<Button-3>", popup) label3.bind("<Button-3>", popup) root.mainloop()

更多推荐

本文发布于:2023-08-07 15:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   右键   上下文   单击   部件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!