PyQT: about button in menu bar - python

How to add an 'about' button to the menu bar of your main window - which when clicked directly opens a dialog with some about text - using PyQT?
Or is that impossible?
Having had a look at the documentation/question and answers online having to do with the menu bar, I get the impression that the QMenuBar only supports triggering events via 'QAction's via menu drop-downs. However I dont want a drop-down for the about button but rather would like it to trigger some showAboutDialog method.
If you have any ideas/links please let me know.

You can add a QAction object directly to the menubar of your MainWindow. Use the QMenuBar.addAction() method for this:
class YourMainWindow(QMainWindow):
def __init__(self):
super().__init__()
menu = QMenuBar()
menu.addAction(show_about_dialog_action)
self.setMenuBar(menu)

Related

How to create a dropdown menu on clicking Qaction in PyQt5?

I am making an application where I want to create a dropdown menu if anyone clicks on the Qaction.
My code
self.navtb = QToolBar("Navigation")
self.navtb.setIconSize(QSize(25, 25))
self.navtb.setMovable(False)
self.addToolBar(self.navtb)
option_btn = QAction(QIcon(os.path.join('images', 'options.png')), "Option", self)
self.navtb.addAction(option_btn)
A QAction can have an associated menu which you can simply set with setMenu(). This menu will pop up (drop down) on click+hold.
Now all you need is to set your button to directly go for the menu on click by altering its popup mode. Note that while the wording pop up is used, the menu will be a proper drop-down menu in a toolbar scenario.
In your example it would roughly translate to:
option_btn.setMenu(...)
self.navtb.widgetForAction(option_btn).setPopupMode(QToolButton.InstantPopup)
For reference, this is how I do it in my code in C++:
// initialize compute menu and let button display menu without holding mouse
actionComputeDisplay->setMenu(new QMenu(widget));
auto btn = qobject_cast<QToolButton*>(toolBar->widgetForAction(actionComputeDisplay));
btn->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup);

Removing a widget by invoking from a button which is set using QSplitter() in PyQt4

I want to make the "Application 1" disappear when the "Single" button is clicked. And show it again i mean both when "Split" button is clicked. Is there any method to make disappear/collapse a widget from QSplitter().
Here's the basic layout :
Thanks in advance.
QWidget has functions show() and hide(), if Application1 is inside QWidget or any other widget inheriting QWidget, you can call hide on the object of that widget when user clicks on Single button, (widget.hide()). When user clicks on Split button you can call show() on the same object to show the widget.
Edit
Another way of achieving this would be:
to set the size of QSplitter. When Single Button is pressed, do the following:
splitter.setSizes([self.width(), 0])
When split button is pressed do the following:
splitter.setSizes([self.width()/2, self.width()/2])
Assuming that the self refer to mainWindow containing the splitter and self.width() gives the width of mainWindow.

Python PyQt QtoolbuttonPopup mode

From what i understood from the internet resources, I could create a Popup menu of QActions on the Qtoolbar by using Qtoolbuttonpopup mode.
So, I created a QMenu and added a few QActions to it by using QMenu.addAction.
After that i have created a QToolButton and set the ToolButtonPopupMode to 2. Followed by setting the QMenu i have created above as the menu for it by using .setMenu(QMenu)
SettingMenu = QtGui.QMenu()
SettingMenu.addAction(Action1)
SettingMenu.addAction(Action2)
SettingButton = QtGui.QToolButton()
SettingButton.setIcon(QtGui.QIcon(QtGui.QPixmap(':/setting.png')))
SettingButton.ToolButtonPopupMode(2)
SettingButton.setMenu(SettingMenu)
from the above code, i am expecting to have a Qtoolbutton on my toolbar and when i click on it, it should pop up a menu with 2 Actions. But when i run the code, all i see is a Qtoolbutton on my toolbar but when i click the Qtoolbutton it doesn't create any pop up menu.
Am i doing this wrong? How do i create a toolbutton that create a pop up menu of actions upon user click?
ToolButtonPopupMode is an enumerating type. All the values in that enum are instances of that type. Because it inherits from int, calling it with an integer returns the same integer. However, you want to set the popupMode property, so use setPopupMode(2).

How to pass along menu shortcuts from child widget holding focus in PyQt4?

I have a QMainWindow with a menu bar, including menu items for Save, Open and Quit with the usual shortcuts. It creates a QTableWidget that lists a bunch of different categories from which the user can choose (at his option).
If the user clicks into the QTableWidget to change categories, the widget takes the focus. That's mostly what I want, but unfortunately it also seems to steal the menu shortcuts, so that pressing Ctrl+S no longer triggers the save.
I experimented with keyPressEvent to solve this, but it seems like overkill even if I do get it working. Isn't there a way to delegate all the control/menu keys back to the QMainWindow ?
There must be an issue with how you are creating your QMenuBar. Here is an example that works just fine for me. The Save continues to function regardless of focus being in the table:
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.resize(640,480)
menuBar = self.menuBar()
menu = menuBar.addMenu("&File")
action = menu.addAction("&Save", self.doAction)
action.setShortcuts(QtGui.QKeySequence.Save)
self.view = QtGui.QTableWidget(5,5)
self.setCentralWidget(self.view)
def doAction(self):
print "Save"

wxPython: how to make taskbar icon respond to left-click

Using wxPython, I created a taskbar icon and menu.
Everything works fine (in Windows at least) upon right-click of the icon: i.e., the menu is displayed, and automatically hidden when you click somewhere else, like on Windows' taskbar.
Now I do want to have the menu appear when the icon is left-clicked as well.
So I inserted a Bind() to a left-click in the Frame class wrapper, calling the CreatePopupMenu() of the taskbar icon:
import wx
class BibTaskBarIcon(wx.TaskBarIcon):
def __init__(self, frame):
wx.TaskBarIcon.__init__(self)
self.frame = frame
icon = wx.Icon('test_icon.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(icon, "title")
def CreatePopupMenu(self):
self.menu = wx.Menu()
self.menu.Append(wx.NewId(), "dummy menu ")
self.menu.Append(wx.NewId(), "dummy menu 2")
return self.menu
class TaskBarFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, style=wx.FRAME_NO_TASKBAR)
...
self.tbicon = BibTaskBarIcon(self)
wx.EVT_TASKBAR_LEFT_UP(self.tbicon, self.OnTaskBarLeftClick)
...
def OnTaskBarLeftClick(self, evt):
self.PopupMenu(self.tbicon.CreatePopupMenu())
...
def main(argv=None):
app = wx.App(False)
TaskBarFrame(None, "testing frame")
app.MainLoop()
This works fine, except that the menu does not disappear automatically when you click somewhere else on your screen. In fact, left-clicking multiple times on the icon creates multiple menus. The only way to hide the menu(s) is to click on one of its items (which you don't always want). I've looked at the available methods of TaskbarIcon, but I failed to be clear about which one to use to hide the menu (.Destroy() didn't work). Moreover, I don't know which event to bind it to (there is a EVT_SET_FOCUS, but I couldn't find any EVT_LOOSE_FOCUS or similar).
So, how to hide the menu upon losing focus?
EDIT: I've inserted a bit more code, to make it more clear
Ah, I've discovered what went wrong. In the statement
self.PopupMenu(self.tbicon.CreatePopupMenu())
I had bound the popup menu to the frame, instead of to the taskbar icon.
By changing it to:
self.tbicon.PopupMenu(self.tbicon.CreatePopupMenu())
all is working well now.
Thanks for all remarks
I think the problem here is that the PopupMenu is usually used in a program's context, not a little icon in the system tray. What that means is that in a normal frame, the popup menu would detect the click the you clicked off of it. Here, you are clicking outside of the wxPython program. Also, the PopupMenu is usually used with EVT_CONTEXT_MENU, not this taskbar event.
You can try wx.EVT_KILL_FOCUS and see if that works since it should theoretically fire when you click off the menu. Or you could ask on the official wxPython forum here: http://groups.google.com/group/wxpython-users/topics
Mike Driscoll
Blog: http://blog.pythonlibrary.org

Categories