按钮单击不适用于我在Python中的第二个类/布局

时间:2018-02-13 07:53:21

标签: python python-3.x pyqt5

我使用的是PyQt5,我设计了3种布局。

  • 登录
  • 信息中心
  • 设置。

LoginDashboard页面包含单独的类文件。当我尝试登录时,它会登录。之后,我尝试点击settings中的dashboard按钮,但没有任何反应。布局没有改变。

然而,如果我单独运行dashboard文件,则单击settings按钮可以正常工作并且布局会发生变化。我很困惑,为什么会这样。这是我的一段代码:

登录:

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_())

仪表板:

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_())

我真正想要的是什么

1 个答案:

答案 0 :(得分:0)

在击败时:

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

你正在划分你的自定义类init,在那里连接按钮click -signal - 只需使用:

self.dashboardWindow = dashboardClass()

或手动添加连接:

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

遵循以下所有条款:

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

请注意,您的命名非常混乱,我无法理解为什么您的开放式DASHBOARD会隐藏并显示您的设置,反之亦然。您可能也会遇到一些问题,但我希望这会解决您的按钮点击问题。

修改

这是一个简单的工作示例,其中三个Windows互相调用(重组):

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 )

只需在main中调用LoginWindow - 类:

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

如果你使用hide - 选项,你可以通过添加这样的内容来重新显示上一个窗口(例如内部SettingWindow - 类):

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

最后,对于没有切换窗口的解决方案,仅使用一个,请查看我的答案:
Switching between frames without erasing contents of each frame