Python GUI 在整个过程完成之前不会更新

发布于 2024-10-16 06:12:36 字数 1759 浏览 5 评论 0原文

我有一个进程从目录中获取文件并将它们放入列表中。然后它循环迭代该列表。循环的最后一行是它应该更新我的 gui 显示的位置,然后它再次从列表中的下一项开始循环。

我的问题是,在整个过程完成之前,它实际上不会更新 gui,这取决于列表的大小,可能需要 30 秒到一分钟多。这给人一种程序被“挂起”的感觉。

我想要它做的是处理列表中的一行,更新 GUI,然后继续。我哪里做错了?更新列表的行是#用驱动器内容填充列表视图。打印语句仅用于调试。

def populateList(self):
    print "populateList"

    sSource = self.txSource.Value
    sDest = self.txDest.Value

    # re-intialize listview and validated list
    self.listView1.DeleteAllItems()
    self.validatedMove = None
    self.validatedMove = []

    #Create list of files
    listOfFiles = getList(sSource)    

    #prompt if no files detected
    if listOfFiles == []:
        self.lvActions.Append([datetime.datetime.now(),"Parse Source for .MP3 files","No .MP3 files in source directory"])

    #Populate list after both Source and Dest are chosen
    if len(sDest) > 1 and len(sDest) > 1:     
        print "-iterate listOfFiles"

        for file in listOfFiles:
            sFilename = os.path.basename(file)
            sTitle = getTitle(file)
            sArtist = getArtist(file)
            sAlbum = getAblum(file)

            # Make path = sDest + Artist + Album
            sDestDir = os.path.join (sDest, sArtist)
            sDestDir = os.path.join (sDestDir, sAlbum) 

            #If file exists change destination to *.copyX.mp3
            sDestDir = self.defineDestFilename(os.path.join(sDestDir,sFilename))

            # Populate listview with drive contents       
            self.listView1.Append([sFilename,sTitle,sArtist,sAlbum,sDestDir])

            #populate list to later use in move command
            self.validatedMove.append([file,sDestDir])
            print "-item added to SourceDest list"
    else:
        print "-list not iterated"

I have a process that gets a files from a directory and puts them in a list. It then iterates that list in a loop. The last line of the loop being where it should update my gui display, then it begins the loop again with the next item in the list.

My problem is that it does not actually update the gui until the entire process is complete, which depending on the size of the list could be 30 seconds to over a minute. This gives the feeling of the program being 'hung'

What I wanted it to do was to process one line in the list, update the gui and then continue. Where did I go wrong? The line to update the list is # Populate listview with drive contents. The print statements are just for debug.

def populateList(self):
    print "populateList"

    sSource = self.txSource.Value
    sDest = self.txDest.Value

    # re-intialize listview and validated list
    self.listView1.DeleteAllItems()
    self.validatedMove = None
    self.validatedMove = []

    #Create list of files
    listOfFiles = getList(sSource)    

    #prompt if no files detected
    if listOfFiles == []:
        self.lvActions.Append([datetime.datetime.now(),"Parse Source for .MP3 files","No .MP3 files in source directory"])

    #Populate list after both Source and Dest are chosen
    if len(sDest) > 1 and len(sDest) > 1:     
        print "-iterate listOfFiles"

        for file in listOfFiles:
            sFilename = os.path.basename(file)
            sTitle = getTitle(file)
            sArtist = getArtist(file)
            sAlbum = getAblum(file)

            # Make path = sDest + Artist + Album
            sDestDir = os.path.join (sDest, sArtist)
            sDestDir = os.path.join (sDestDir, sAlbum) 

            #If file exists change destination to *.copyX.mp3
            sDestDir = self.defineDestFilename(os.path.join(sDestDir,sFilename))

            # Populate listview with drive contents       
            self.listView1.Append([sFilename,sTitle,sArtist,sAlbum,sDestDir])

            #populate list to later use in move command
            self.validatedMove.append([file,sDestDir])
            print "-item added to SourceDest list"
    else:
        print "-list not iterated"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仄言 2024-10-23 06:12:36

这是 GUI 程序的常见问题。在接收并处理“重绘”命令之前,控件不会更新,并且在函数返回之前不会发生这种情况。

您可以通过调用其 随时强制控件重新绘制更新方法,如该问题的答案所示:如何强制刷新 wx.Panel?

This is a common problem with GUI programs. Controls don't get updated until a "repaint" command is received and processed, and that won't happen until your function returns.

You can force a control to repaint at any time by calling its Update method, as shown in the answer to this question: How do you force refresh of a wx.Panel?

ゞ花落谁相伴 2024-10-23 06:12:36

创建一个工作线程/进程,在后台进行处理并在处理完成后更新 GUI,可能会在工作期间报告进度。

查看 线程多处理 模块。

Create a worker thread/process that does your processing in the background and updates the GUI after the processing is done, maybe reporting progress during work.

Have a look at the threading or multiprocessing modules.

不弃不离 2024-10-23 06:12:36

我可能建议您尝试 wx.lib.delayedresult。它在某种程度上是简化的多线程解决方法。您可以将业务逻辑放入工作函数中,将其他逻辑(包括GUI追加、更新)放入消费者函数中。工作函数在另一个线程中运行,而消费者函数保证在主线程中的工作函数完成后运行。

I might suggest you try wx.lib.delayedresult. It is somehow the simplified multithread workaround. You can put your business logic into worker function and other logics (including GUI appending, updating) in consumer function. The worker function runs in another thread while the consumer function is guaranteed to run after the finish of worker function in main thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文