0

I am using PyQt5 and I have designed 3 layouts.

  • Login
  • Dashboard
  • Settings.

Login and Dashboard pages have individual class files. When I am trying to login, it gets logged in. After that, I am trying to click the settings button from my dashboard, but nothing happens. The layout doesnt change.

Whereas, if I individually run the dashboard file, clicking on the settings button works and the layout changes. I am like confused, why this is happening. Here is a piece of my code:

login:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from login import Ui_loginWindow
from dashboard import Ui_dashboardWindow

class loginClass(Ui_loginWindow):
    def __init__(self,dialog):
        Ui_loginWindow.__init__(self)
        self.setupUi(dialog)
        self.loginBtn.clicked.connect(self.openDashboard)

    def openDashboard(self):
        self.dashboardWindow = Ui_dashboardWindow()
        self.dashboardWindow.setupUi(loginWindow)
        loginWindow.hide() #works
        loginWindow.show() #works

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    loginWindow = QtWidgets.QMainWindow()
    var = loginClass(loginWindow)
    loginWindow.show()
    sys.exit(app.exec_())

dashboard:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from dashboard import Ui_dashboardWindow
from settings import Ui_settingsWindow

class dashboardClass(Ui_dashboardWindow):
    def __init__(self,dialog):
        Ui_dashboardWindow.__init__(self)
        self.setupUi(dialog)
        self.settingsBtn.clicked.connect(self.openSettings)

    def openSettings(self):
        self.settingsWindow = Ui_settingsWindow()
        self.settingsWindow.setupUi(dashboardWindow)
        dashboardWindow.hide() #doesnot work
        dashboardWindow.show() #doesnot work

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    dashboardWindow = QtWidgets.QMainWindow()
    var = dashboardClass(dashboardWindow)
    dashboardWindow.show()
    sys.exit(app.exec_())

what i actually want

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    See [this answer](https://stackoverflow.com/a/11812578/984421) for a complete login dialog example. – ekhumoro Feb 13 '18 at 19:23
  • the problem is its a single file containing 2 classes whereas i am using 2 separate files and with 2 different classes. my question is: when i am running login.py, the button 1 on page login.py(layout 1) works which leads to dashboard.py page(layout 2). there if i click button 2, nothing happens. how to correct this? –  Feb 14 '18 at 04:16
  • Just re-write your code to use the solution I suggested. If you want two modules, import one into the other. – ekhumoro Feb 14 '18 at 18:29
  • import can be done but how to import dashboardClass() to loginClass() which have different layout? it says: "NameError: name 'dashboardClass' is not defined". I have followed your example. –  Feb 15 '18 at 06:11

1 Answers1

0

while excuting:

self.dashboardWindow = Ui_dashboardWindow()
self.dashboardWindow.setupUi()

you are sciping your custom-class init, where you connect the buttons click -signal - simply use:

self.dashboardWindow = dashboardClass()

or add the connect manually:

self.dashboardWindow = Ui_dashboardWindow()
self.dashboardWindow.setupUi()
self.dashboardWindow.settingsBtn.clicked.connect(openDashboard)

follow all this with:

self.dashboardWindow.show()
self.hide()

Please note that your naming is very confusing and I can't follow why your open DASHBOARD hides ands shows your SETTINGS and the other way around. You probably will have some issue there too, but I hope this will address your button-click-problem.


EDIT:

Here is a simple working example with three Windows called inside each other (restructured):

from PyQt5.QtWidgets import *
class LoginWindow( QMainWindow ) :
    def __init__ ( self, parent = None ) :
        super( LoginWindow, self ).__init__( parent )
        self.text = QLineEdit( 'user/password', self )
        self.loginBtn = QPushButton( 'login; dashboard', self )
        self.loginBtn.move( 0, 50 )
        self.loginBtn.clicked.connect( self.openDashboard )

    def openDashboard ( self ) :
        self.dashboard = DashboardWindow( self )
        self.dashboard.show()
        #self.hide()  # optional

class DashboardWindow( QMainWindow ) :
    def __init__ ( self, parent = None ) :
        super( DashboardWindow, self ).__init__( parent )
        self.settingsBtn = QPushButton( "open settings", self )
        self.settingsBtn.clicked.connect( self.openSettings )

    def openSettings ( self ) :
        self.settings = SettingWindow( self )
        self.settings.show()
        #self.hide()  # optional

class SettingWindow( QMainWindow ) :
    def __init__ ( self, parent = None ) :
        super( SettingWindow, self ).__init__( parent )
        self.label = QLabel( 'hi :)\nsettings here', self )

where the LoginWindow-class is simply called in the main:

import sys
if __name__ == "__main__" :
    app = QApplication( sys.argv )
    login = LoginWindow()
    login.show()
    sys.exit( app.exec_() )

if your using the hide-option, you can reshow the previous window by adding something like this (example for inside SettingWindow-class):

def closeEvent ( self, *args, **kwargs ) :
    self.parent().show()
    super( SettingWindow, self ).closeEvent( *args, **kwargs )

Finally, for a solution without switching the windows, using just one, have a look at my answer in:
Switching between frames without erasing contents of each frame

Skandix
  • 1,916
  • 6
  • 27
  • 36
  • my openDashboard() doesnot hide/show SETTINGS. That is the problem. my first transition from login to dashboard is happening but from dashboard to settings is not happening. can you plz let me know why or how to make this happen? –  Feb 13 '18 at 09:34
  • @amitavadeb: try using the class approach: `self.dashboardWindow = dashboardClass()`. But still: using `hide()` and `show()` rigth after an other doesn't seems reasonable. – Skandix Feb 13 '18 at 09:52
  • ok but you didnot answer why my first class "loginClass" is working and my second class "dashboardClass" is not working. how to make it work? can you tell me how will you write the show/hide code? –  Feb 13 '18 at 10:14
  • i did what you have asked me to do without any luck. i have attached an image. can you tell me how to achieve this? –  Feb 13 '18 at 11:39
  • @amitavadeb I added an example to the answer, as well as a reference to an other question – Skandix Feb 15 '18 at 08:26
  • thanks @Skandix it works but sorry to ask you this question again. can i achieve the same example by separating out the classes to individual files? if yes, how? if no, why? –  Feb 15 '18 at 08:43
  • thanks @Skandix it works and i was able to separate the classes aswell. thanks a lot man. –  Feb 15 '18 at 08:58
  • by replacing the first two line in the class init with the ones that you were using (for example in your dashboard class - posted in your question). I simply did not do that, because I don't have the files, because you did not provided them ... Read this: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Skandix Feb 15 '18 at 09:13