简体   繁体   中英

How Refresh/Re-initialize Widgets in QStackedWidget

I'm trying to create a script that makes API calls to a website to get info (via the requests module). The user can then manipulate the website using my script/gui.

My app's main window will have several buttons at the bottom which acts like tabs to switch between windows(let's say for argument's sake 2 buttons to switch between two windows).

When certain changes are made, I need the QStackedWidget to refresh all the widgets/labels in certain windows that are not currently being displayed.

Summary of my code:

# Global Variables
app = QApplication(sys.argv)
win = QtWidgets.QStackedWidget()
response = {} # global dict to store API response from website


class UiWindowOne(QMainWindow):
    # This window mostly shows information that I get from the website.
    def __init__(self):
        super(UiWindowOne, self).__init__()
        self.setup_ui(self)
        self.retranslate_ui(self)
        # Then I map buttons to methods
    
    def setup_ui(self, WindowOne):
        # This was generated by QT Designer and places widgets
    
    def retranslate_ui(self, WindowOne):
        # This was generated by QT Designer and places widgets
        
        
    def refresh(self):
        '''
        This function refreshes the current window. Basically, I put everything in the __init__ function in here (except "super(UiWindowOne, self).__init__()".
        :return: None
        '''
        self.setup_ui(self)
        self.retranslate_ui(self)
        # Also map buttons to methods

    


class UiWindowTwo(QMainWindow):
    def __init__(self):
        super(UiWindowTwo, self).__init__()
        self.setup_ui(self)
        self.retranslate_ui(self)
        # Then I map buttons to methods
    
    def setup_ui(self, WindowTwo):
        # This was generated by QT Designer
    
    def retranslate_ui(self, WindowTwo):
        # This was generated by QT Designer
        
        
    def refresh(self):
        '''
        This function refreshes the current window. Basically, I put everything in the __init__ function in here (except "super(UiWindowTwo, self).__init__()".
        :return: None
        '''
        self.setup_ui(self)
        self.retranslate_ui(self)
        # Also map buttons to methods

    def update_website(self):
        # Make changes to website
        # After changes were made, I want to get modified info from the website and re-initialize/refresh both windows to reflect the changes made.
        # I can easily call self.refresh() to refresh WindowTwo. But I cannot refresh WindowOne from here.
        

def main():
    # Here I make API calls to the Website to get info/images
    

    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(".\\imgs/static/A.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
    win.setWindowIcon(icon)
    win.setWindowTitle("NAME")
    
    
    first_window = UiWindowOne()
    second_window = UiWindowTwo()
    

    win.addWidget(first_window)
    win.addWidget(second_window)
    

    win.setGeometry(250, 250, 820, 854)
    win.setFixedWidth(820)
    win.setFixedHeight(854)
    win.show()


    sys.exit(app.exec())


if __name__ == '__main__':
    main()      

I have tried doing a "first_window.refresh()" under the update_website() function in UiWindowTwo, but then python tells me that first_window is not defined.

I then tried making first_window and second_window global variables, but then I ended up reordering my whole script and couldn't get it to run.

@musicamante, thank you for the info. Looks like my whole approach to PyQT is wrong. I removed the main function for now and everything works. I will finish my current script as is and then try to do as you suggest by not editing pyuic files. I'll also look at not using QMainWindow in StckedWidget. Thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM