Python3 Tkinter popup menu not closing automatically when clicking elsewhere

2024/10/18 12:47:29

I'm running Python 3.3.3 (and right now I'm on Ubuntu but I also develop on Mac and Windows, which I haven't yet tested). I have a Treeview object that responds to right click on items and shows a context menu depending on what you click... but I've noticed that if you right click somewhere else while the original menu is up, it just opens another one.

In fact, normal clicking doesn't hide them either. Even when I close the window the menus still stay floating. The only way to get them to go away is to click one of the options.

The end result is this: Context Menus EVERYWHERE

My code for the menu is as follows:

def rightclick_listitem(self, event):rowitem = self.sources.identify('item', event.x, event.y)if rowitem == '':print('Right clicked an empty space.')return# user right clicked something.self.sources.selection_set(rowitem)rcmenu = Menu(self.root, tearoff=0)plugin_disabled=self.sources.item(rowitem, 'values')[0] == 'Disabled'if plugin_disabled:rcmenu.add_command(label='Plugin is disabled...',command=self.plugin_disabled_click)rcmenu.add_command(label='Plugin options',state='disabled' if plugin_disabled else 'active')rcmenu.add_command(label='Uninstall plugin')rcmenu.post(event.x_root, event.y_root)

The code that calls this code is located here:

    #RIGHTMOUSE is a variable that changes based on OS due to the way Mac OSX works#sources is the treeview objectself.sources.bind(RIGHTMOUSE, self.rightclick_listitem)

I googled around and only got some people asking the same question with no answers. I'm still somewhat new to tkinter and python in general, and didn't see anything about this. I bind other actions to the treeview as well.

If you need more sourcecode my project is here: https://github.com/Mgamerz/Fresh-Set-of-Images (freshsetofimages.py)

Any help is appreciated.

And the plugins required to make this appear: https://github.com/Mgamerz/fsoi_plugins

Answer

Try calling the method tk_popup rather than post.

Also, your code has a memory leak, in that each time you right-click you're creating a new menu but never destroying the old one. You only ever need to create one, and the reconfigure it before popping it up.

https://en.xdnf.cn/q/72951.html

Related Q&A

How does python process a signal?

What is the workflow of processing a signal in python ? I set a signal handler, when the signal occur ,how does python invoke my function? Does the OS invoke it just like C program? If I am in a C e…

Pandas Dataframe to dict grouping by column

I have a dataframe like this:Subject_id Subject Score Subject_1 Math 5 Subject_1 Language 4 Subject_1 Music 8 Subject_2 …

How can I use a Perl module from Python?

There exists a Perl module that provides the perfect functionality for my Python app. Is there any way for me to utilize it? (it is complicated, it would take me a month to port it)I dont want to hav…

HTTPS log in with urllib2

I currently have a little script that downloads a webpage and extracts some data Im interested in. Nothing fancy.Currently Im downloading the page like so:import commands command = wget --output-docume…

Filter values inside Python generator expressions

I have a dictionary dct for which I want each of its values to be summed provided their corresponding keys exist in a specified list lst.The code I am using so far is:sum(dct[k] for k in lst)In the abo…

Python and tfidf algorithm, make it faster?

I am implementing the tf-idf algorithm in a web application using Python, however it runs extremely slow. What I basically do is:1) Create 2 dictionaries:First dictionary: key (document id), value (lis…

How to use Python to find all isbn in a text file?

I have a text file text_isbn with loads of ISBN in it. I want to write a script to parse it and write it to a new text file with each ISBN number in a new line.Thus far I could write the regular expres…

AWS Batch Job Execution Results in Step Function

Im newbie to AWS Step Functions and AWS Batch. Im trying to integrate AWS Batch Job with Step Function. AWS Batch Job executes simple python scripts which output string value (High level simplified req…

Simplify Django test set up with mock objects

Often when Im writing tests for my Django project, I have to write a lot more code to set up database records than I do to actually test the object under test. Currently, I try to use test fixtures to …

How to use tf.data.Dataset.padded_batch with a nested shape?

I am building a dataset with two tensors of shape [batch,width,heigh,3] and [batch,class] for each element. For simplicity lets say class = 5.What shape do you feed to dataset.padded_batch(1000,shape)…