【问题标题】:How Refresh/Re-initialize Widgets in QStackedWidget如何在 QStackedWidget 中刷新/重新初始化小部件
【发布时间】:2021-10-16 03:17:45
【问题描述】:

我正在尝试创建一个脚本,该脚本对网站进行 API 调用以获取信息(通过请求模块)。 然后用户可以使用我的脚本/gui 来操作网站。

我的应用程序的主窗口底部有几个按钮,它们的作用类似于在窗口之间切换的选项卡(为了论证的缘故,假设有 2 个按钮在两个窗口之间切换)。

当进行某些更改时,我需要 QStackedWidget 来刷新当前未显示的某些窗口中的所有小部件/标签。

我的代码总结:

# 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() 函数下执行“first_window.refresh()”,但随后 python 告诉我 first_window 未定义。

然后我尝试创建 first_window 和 second_window 全局变量,但后来我重新排序了整个脚本并且无法运行它。

【问题讨论】:

  • 1.将appwin 的创建都移动到main,否则if __name__ == '__main__': 将毫无意义,并且还会产生严重的问题; 2.不要将QMainWindows添加到QStackedWidget(我知道有教程这样做,但它们只是错误); 3. NOT 编辑 pyuic 生成的文件,也不要尝试将它们的内容与您的脚本合并;重新创建这些文件并学习如何正确 import 并按照有关 using Designer 的官方指南使用它们; 4.提供正确的minimal reproducible example

标签: python qt-designer qstackedwidget pyqt6


【解决方案1】:

@musicamante,谢谢你的信息。看起来我对 PyQT 的整个方法都是错误的。我现在删除了主要功能,一切正常。我将按原样完成我当前的脚本,然后尝试按照您的建议不编辑 pyuic 文件。我还将研究在 StckedWidget 中不使用 QMainWindow。谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 2012-08-24
    相关资源
    最近更新 更多