簡體   English   中英

如何刷新/重新初始化 QStackedWidget 中的小部件

[英]How Refresh/Re-initialize Widgets in QStackedWidget

我正在嘗試創建一個腳本,使 API 調用網站以獲取信息(通過請求模塊)。 然后用戶可以使用我的腳本/gui 操作網站。

我的應用程序的主要 window 將在底部有幾個按鈕,它們像選項卡一樣在窗口之間切換(為了爭論起見,我們說 2 個按鈕在兩個窗口之間切換)。

當進行某些更改時,我需要 QStackedWidget 刷新某些 windows 中當前未顯示的所有小部件/標簽。

我的代碼摘要:

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

我曾嘗試在 UiWindowTwo 的 update_website() function 下執行“first_window.refresh()”,但隨后 python 告訴我未定義 first_window。

然后我嘗試制作 first_window 和 second_window 全局變量,但最終我重新排序了我的整個腳本並且無法運行它。

@musicamante,謝謝你提供的信息。 看起來我對 PyQT 的整個方法都是錯誤的。 我暫時刪除了主要的 function,一切正常。 我將按原樣完成我當前的腳本,然后嘗試通過不編輯 pyuic 文件來按照您的建議進行操作。 我還將研究在 StckedWidget 中不使用 QMainWindow。 謝謝你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM