如何在QStackedWidget中刷新/重新初始化Widget

2024-06-07 01:05:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的应用程序的主窗口底部会有几个按钮,它们就像标签一样在窗口之间切换(比如说,为了论证起见,有两个按钮在两个窗口之间切换)

当进行某些更改时,我需要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\u website()函数下执行“first\u window.refresh()”,但python告诉我first\u window没有定义

然后我尝试创建第一个窗口和第二个窗口的全局变量,但后来我重新排列了整个脚本,无法运行它


Tags: thetofromselfuiinitdefsetup
1条回答
网友
1楼 · 发布于 2024-06-07 01:05:12

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

相关问题 更多 >