I just started trying to build simple GUIs because I’ve always used command-line script and wanted to explore more. I’m using the latest versions of both PyQt4 and Python 3.
The problem is that given the code below, on clicking the button in the first window, the second won’t show up but I can’t get why. What am I missing? The script is fully functional (except, of course, for the second window part) so it’s just a copy/paste to try it.
import sys
from PyQt4 import QtCore, QtGui
class GreetWindow(QtGui.QMainWindow):
def __init__(self):
# main init
QtGui.QMainWindow.__init__(self)
self.setWindowTitle('File Checker!')
self.centralWidget = QtGui.QWidget()
self.tLabel = QtGui.QLabel('Hello, and welcome! Please, close me!', self.centralWidget)
self.bClose = QtGui.QPushButton('Close', self.centralWidget)
# layout
self.lVBox = QtGui.QVBoxLayout()
self.lVBox.setSpacing(2)
self.lVBox.addWidget(self.tLabel)
self.lVBox.addWidget(self.bClose)
# signal connections
self.connect(self.bClose, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('close()'))
self.connect(self.bClose, QtCore.SIGNAL('clicked()'), self.showSecond)
# layout combination
self.centralWidget.setLayout(self.lVBox)
self.setCentralWidget(self.centralWidget)
def showSecond():
self.theSecond = SecondWindow()
self.theSecond.show()
class SecondWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle('This is the second!')
self.centralWidget = QtGui.QLabel('Mission accomplished')
self.setCentralWidget(self.centralWidget)
def main():
app = QtGui.QApplication(sys.argv)
ex = GreetWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Btw, I really appreciate critics on the way this simple script is coded. I’m starting now with GUIs and I’d like to start in the most pythonic way possible. Thanks all!
Make sure to run your application in a console, so you can see the error output:
And indeed, there is an error in the declaration of
showSecond: noself! Should be like this: