2

I'm not able to modify an attribute of the first class (login_window) from the other (user_register_window)... My code is the following:

class login_window(QtGui.QWidget, Ui_login_form):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setupUi(self)
        self.register_window = None
        self.login_btn.clicked.connect(self.login_func)
        self.register_btn.clicked.connect(self.register_func)
        usernames_list = c.execute("SELECT USERNAME FROM register_table").fetchall()
        for data in usernames_list:
            self.username_combo_field.addItems(data)
   def register_func(self):
        if self.register_window is None:
            self.register_window = user_register_window(self)
            self.register_btn.setEnabled(False)
            self.login_btn.setEnabled(False)
        self.register_window.show()



class user_register_window(QtGui.QDialog, Ui_register_form):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setupUi(self)
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.CustomizeWindowHint)
        self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowContextHelpButtonHint)
        self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowCloseButtonHint)
        self.cancel_btn.clicked.connect(self.cancel_pressed)

    def cancel_pressed(self):
        user_register_window.close(self)
        login_window.login_btn.setEnabled(True)  #doesn't work

My login_window runs first, so when I click register_btn, "user_register_window" appears, with register_btn and login_btn disabled in login_window. So far everything works ok, my problem is when i close "user_register_window". When I press cancel_btn I would like to enable again "register_btn" and "login_btn".

I tried with the following code:

login_window.login_btn.setEnabled(True)

inside cancel_pressed function, but it doesn't work.

AttributeError: type object 'login_window' has no attribute 'login_btn'

Is there a way to do it?

Thanks a lot for your help!!!

1 Answer 1

2

The call login_window.login_btn.setEnabled(True) is not able to reach the attribute because you refer to the class, and not an instance (and the attribute is set on instance only, inside the constructor).

You can access the instance login_window from the second by using the parent link, as you've declared the second as a child widget (with self.register_window = user_register_window(self)), you can use the following lines to achieve this :

self.parent().login_btn.setEnabled(True)
self.parent().register_btn.setEnabled(False)

#and it's better to close the widget with : 
self.close()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.