PyQt Clear the menubar - python

Is there a quick way to clear the menu bar i.e. Remove all menus in PyQt.
The reason I ask is that I am developing a modular application. Each module provides it's own menus and gives a weighting to where that menu should appear. Each module can also add a menuAction to another modules menu, however when that happens it changes the order of the menus. What i want to do is when the menus have finished being created is to remove them all and then re add them.
I know this will work because I can re add them now and they appear in the correct order but all the previous menus are still there.

did you try QMenu.clear (self)
mymenu = QtGui.QMenu("Menu")
mymenu.clear()

Related

Qt with Python: how to route shortcuts to main menu? [duplicate]

I am building a Qt application on Linux. I have a menu bar in the main window with two menus in it, each with several actions, all of which have keyboard shortcuts associated with them. The keyboard shortcuts work when the menus are not open, but when one of the menus is open, none of them work.
The shortcuts were added to the actions with setShortcut prior to the actions being added to their respective menus with [menuobject]->addAction. All the actions have the main window as their parent. After reading QAction shortcut doesnt always work I added calls to addAction, adding the action to the main window. This did not correct the problem.
Example of the code for one of the menu items:
//In the main window constructor
gameQuit = new QAction(QString(tr("&Quit\tCtrl+Q")), this);
gameQuit->setShortcut(QKeySequence(Qt::Key_Q | Qt::CTRL));
addAction(gameQuit);
connect(gameQuit, SIGNAL(triggered()), this, SLOT(close()));
gameMenu = menuBar()->addMenu(QString(tr("&Game")));
gameMenu->addAction(gameQuit);
In QtCreator, which I assume was written with Qt, the keyboard shortcuts for the menu items do work when the menus are open, so I think there must be a way.
Thanks for any help.
Taking some advice from the comments of the cited post (which had been rebuked, which is why I didn't try it initially), I modified the shortcut context using [actionobject]->setShortcutContext(). Apparently the default does not work in my scenario.
I first tried setting to Qt::WindowShortcut, which didn't work. Qt::ApplicationShortcut did work, however, this may have shortcomings as noted in the comments of the cited post. They don't happen to matter for this particular application of mine though, so I am going to post and accept this as the answer.
Example of the correcting code:
//In the constructor of the main window, after creation of the action and
//setting of the shortcut
gameQuit->setShortcutContext(Qt::ApplicationShortcut);

TclError: No more menus can be allocated

So I'm making a program in Python and for user interface I'm using Tkinter, the application seems to be running fine but as I navigate a while in it, I get the follwing TclError:
TclError: No more menus can be allocated.
I did some research and I found out that there is a limit for these "menus" on windows, but my question is, how do I know that these menus are being created? How can I delete them when I stop using them? I use methods for deleting top menus as "top.destroy()". But even though I am generating more and more menus and it's overflowing the windows heap or whatever it is.
Here is the source code for my program:
https://github.com/molBR/upgestao
Can somebody help?
Thanks in advance.
I have the same error when creating auto-hide menu. At first I created automatically generated and deleted dummy (empty) menu bar to replace a real menu bar to hide it. So after several dozens hiding tkinter start to show me this error: TclError: No more menus can be allocated.
The solution is simple: do not create/delete menus automatically.
Just create all necessary menus you want and save them in the memory. Do not delete them. And switch between menus during work of your program.

How to set QMenu tear-off window title?

I'm creating a custom ui in Maya 2017 which uses PyQt5 (well... technically PySide2, but it's essentially the same).
I've got a few CustomContextMenu popup menus that I've created in my ui and I've used popup.setTearOffEnabled(True) to be able to tear them off into a separate window (popup, being the QMenu item).
I cannot seem to figure out how to set the title for the resulting torn off window. Currently, each torn off window is titled "Maya-2017", but I'd like to give it a unique name for clarity. I've noticed that Maya's menu items with tear off functionality name the resulting window with the menu's name, so it would seem this is doable. Am I just missing something obvious?
I have tried using popup.setTitle('test name') on the QMenu thinking it would then name the tear off window this title, but it doesn't seem to do anything. Other than that, I'm at a loss.
I'm not sure whether torn-off menus appear the same on all platforms, but on my Linux system, they are shown as tool windows with a title-bar. So the title can be set like this:
menu = QMenu('File')
menu.setTearOffEnabled(True)
menu.setWindowTitle('File')

python drop down menu attached to the windows taskbar

I'm currently working on a timer application on python 3, basically is for you to keep track of how many time spend doing something. I'm in the part of the process where i need to create an interface probably with tkinter, with a drop down menu that lists all the things i need to keep track of (lets call those things "statuses").
Now, my question is, is there a way to add that the drop down menu with the statuses in the task bar on windows? so that a user does not have to go the interface and change status, but most importantly, the current status can be always on sight.
Is this even possible? If it is, I just need a few hints please! :)
Am not sure about adding a drop down menu to the taskbar icon(like the one you get with Microsoft lync taskbar icon to change status, hope u need something like this), but i would suggest(incase this helps) to add menu at right click over the taskbar icon.
wxPython might help.
see this article.
http://codeboje.de/MailSneaker-Part-3-SystemTrayTaskBar-Icons-with-Python-and-wxPython/
Hope this helps.

Creating an interconnected menu in Python

So I'm teaching myself Python/Pygame, and to start off I'm trying to make a basic menu that the user can scroll through and then select an item to go to a different menu. My initial thought was to define the menus as a tuple of menu items, with each item being a tuple of what text to display and where selecting that item leads you. So, for example:
mainmenu = (("Go to Menu 1",menu1),("Go to Menu 2",menu2))
menu1 = (("Go to Menu 2",menu2),("Return to Main Menu",mainmenu))
menu2 = (("Go to Menu 1",menu1),("Return to Main Menu",mainmenu))
then have a function like makeMenu(menu) that prints out a given menu, and allows the user to scroll through it. When the user makes their selection, makeMenu(menu) is called again, this time being passed the menu that corresponds to the currently selected option.
The problem is that Python won't allow you to define mainmenu in terms of other menus that haven't been defined yet. This makes sense, but I can't think of a way to create a series of interconnected menus without defining them relative to each other. I'm sure there's a simple solution, but for the life of me I can't figure it out. Any thoughts?
Often, for menus, I like to use the State Pattern. A fully fledged one may be overkill for your current needs - but it should lead you in the right direction. An example of this for C, specifically talking about game states, is available on gamedevgeek.com.
This may not directly answer your questions, but it should lead you to a much more extensible and robust solution.

Categories

Resources