Skip to main content
 首页 » 编程设计

python之如果添加多个窗口(pyside),进度条将无法正常工作

2025年05月04日13wayfarer

我有一个主窗口,当用户选择要上传的文件时,新窗口将打开文件的显示状态,即文件名、上传速度、剩余时间等(如 IDM 和 xdman)

当用户添加一个文件上传所有的过程也是如此

如果用户想要添加更多文件上传,就会出现问题

第一个窗口将停止更新数据(应该继续直到上传完成)

第二个窗口显示继续更新信息,但进度条将与第一个窗口混合(我的意思是两个进度条将同时显示在第二个窗口中!!!) 我应该如何将它们分开? 这是我的代码的简短版本,以了解我所做的事情!

class Window(QtGui.QWidget): 
 def __init__(self, parent=None): 
    super(Window, self).__init__(parent) 
    self.table = QtGui.QTableWidget(self)#progressbar work perfectly in table but not in new windows ! 
 
 def handleAddUpload(self): 
    #choosing a file codes go here 
 
    progress = QtGui.QProgressBar(self.table) 
    progress.setRange(0, len(data)) 
    #two above line will add progressbar to table ! 
    tabs = self.createTab() #caling createTab for creating new window for... 
 
 def handleUploadProgress(self, key, sent, total): 
 #handleeUploadProgress method call rapidly since data needs to be stream and update ! 
 
 
   progress = self.table.cellWidget(key, 0) 
   progress.setValue(sent) 
#two above line will update value in table without any problem 
 
   self.procbar.setValue(sent) #this line call progressbar value from createTab() 
 
 def createTab(self): #all my problems are in this method 
    self.tab = QtGui.QTabWidget() 
    layout = QtGui.QVBoxLayout(self) 
    tab = QtGui.QWidget() 
    layout1 = QtGui.QVBoxLayout(tab) 
    self.procbar = QtGui.QProgressBar() 
    self.procbar.setStyleSheet("") 
    self.procbar.setRange(0, 0) #a range added in handleaddupload,for shorting this I didn't show that 
    self.procbar.setValue(0) 
 
    layout1.addWidget(self.procbar) 
    self.tab.addTab(tab, 'Upload Status') 
    layout.addWidget(self.tab) 
    self.tab.show() 
 
 def handleUploadFinished(self, key): 
    #codes..... 

我想这就是所有与进度条有关的东西了

因为我是 python/pyside 的新手,所以我寻求帮助和 here is the base of my code (我从这个答案开始)

这是我的简短问题:如何添加单独的唯一小部件以同时显示多个进度条?

感谢大家阅读这个无聊的问题

更新 1: 这是软件的图片

当我添加 Pharrell 歌曲上传表格中的进度条和单独的窗口显示时,但是当我添加新歌曲上传时(贾斯汀):

1- Pharrell 的单独窗口中的进度条停止工作(如您所见,百分比不一样)!

新添加歌曲的 2 进度条(仅在单独的窗口中)显示进度百分比(它迅速变为 10% 和 26%)

(在所有情况下表中的进度条都有效)

请您参考如下方法:

据我从您发布的代码中可以看出,问题似乎是您没有正确管理状态窗口。

首先,我认为您应该像这样创建一个 StatusTab 类:

class StatusTab(QtGui.QTabWidget): 
    def __init__(self, parent=None): 
        QtGui.QTabWidget.__init__(self, parent) 
        layout = QtGui.QVBoxLayout(self) 
        tab = QtGui.QWidget(self) 
        layout1 = QtGui.QVBoxLayout(tab) 
        self.procbar = QtGui.QProgressBar(tab) 
        self.procbar.setStyleSheet("") 
        self.procbar.setRange(0, 0) 
        self.procbar.setValue(0) 
        layout1.addWidget(self.procbar) 
        self.addTab(tab, 'Upload Status') 
        layout.addWidget(tab) 
        ... 

(当然,这个类还会有其他方法和属性来处理上传状态、信息等)。

接下来,您将需要一个容器来保存 StatusTab 的实例:

class Window(QtGui.QWidget): 
    def __init__(self, address): 
        QtGui.QWidget.__init__(self) 
        ... 
        self._uploaders = {} 
        self._statustabs = {} 

然后像这样创建它们:

    def handleAddUpload(self): 
        ... 
        statustab = StatusTab(self) 
        self._statustabs[row] = statustab 

像这样更新它们:

    def handleUploadProgress(self, key, sent, total): 
        ... 
        statusbar = self._statusbars[key] 
        statusbar.procbar.setValue(sent) 

最后,一旦不再需要它们,您应该删除它们:

    def handleUploadFinished(self, key): 
        ... 
        uploader = self._uploaders.pop(key) 
        uploader.deleteLater() 
        statustab = self._statustabs.pop(key) 
        statustab.deleteLater() 

显然,上面的代码是基于我的另一个答案,但希望您可以根据自己的需要进行调整。