[PyQt] Help with help action

Mark Summerfield mark at qtrac.eu
Thu Sep 27 08:45:59 BST 2007


On 2007-09-26, Thorsten Kampe wrote:
> Hi,
>
> I'm trying to teach myself GUI programmming with the help of the new
> PyQt book and the examples from the PyQt package. Although I've been
> doing Python for five years I have a hard time doing OOP stuff.
> Frankly, I never know when and where to put this "self" thing.

My book assumes that you're comfortable with the basics of OOP (and does
mention this in the first para of the Introduction), so it will be a bit
of a climb for you! On page 27 I recommend "Core PYTHON Programming" by
Wesley Chun for slower/gentler coverage of Python itself---he starts
covering OOP from chapter 13, whereas I do it in chapter 3.

> My script (see below) works (no wonder, it's cut down from the
> application.pyw example). But as soon as I wanted to add a help action
> it doesn't work. The help action is ripped from Mark Summerfield's
> sandbox.pyw and calculate.pyw.
>
> Can anyone help me with the help function?!

I think you need to read a bit more:-)

The key point is that if you want a top-level window (main window or
dialog), 99% of the time you will use a QMainWindow or QDialog subclass.

Below is a fixed version of your code with a few bits added (and marked
as such). Note that in the dialogs chapter (and in one of the examples I
provide), I mention that you can create dialogs from scratch in methods
(as I've done below to minimise the changes to your code), but that I
don't recommend this. I think it is better to create a separate class
for each dialog (and there are many examples of this discussed and shown
in chapters 4 and 5 in particular). For info on DeleteOnClose see either
"deleting windows, automatically" or "Qt.Widget Attribute,
WA_DeleteOnClose" in the index. 


#!/usr/bin/env python

import sys
from PyQt4 import QtGui, \
                  QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        # create actions
        self.aboutAct = QtGui.QAction(self.tr('&About'), self)
        self.aboutAct.setStatusTip(
		self.tr('Show the application\'s About box'))
        self.connect(self.aboutAct, QtCore.SIGNAL('triggered()'),
		self.about)

        self.helpAct = QtGui.QAction(self.tr('&Help'), self)
        self.helpAct.setStatusTip(self.tr('Online help'))
        self.connect(self.helpAct, QtCore.SIGNAL('triggered()'), self.help)

        # create menus
        self.helpMenu = self.menuBar().addMenu(self.tr('&Help'))
        self.helpMenu.addAction(self.helpAct)
        self.helpMenu.addAction(self.aboutAct)

        # create status bar
        self.statusBar().showMessage(self.tr('Ready'))
	self.setWindowTitle("Test App") # Added for neatness

    def about(self):
        QtGui.QMessageBox.about(self, self.tr('About Application'),
		self.tr('Application does something\n'
                        'This software comes without warranty, '
			'liability or support!'))

    def help(self):
	helpDialog = QtGui.QDialog(self) # Added
	helpDialog.setAttribute(QtCore.Qt.WA_DeleteOnClose) # Added
        browser = QtGui.QTextBrowser()
        browser.append('Help')
        layout = QtGui.QVBoxLayout()
        layout.addWidget(browser)
	helpDialog.setLayout(layout) # Added
	helpDialog.setWindowTitle("Test App - Help") # Added for neatness
	helpDialog.show() # Changed

app     = QtGui.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
app.exec_()


-- 
Mark Summerfield, Qtrac Ltd., www.qtrac.eu



More information about the PyQt mailing list