質問

I'm new to Python (and, indeed, new to PC development) and would like to know how I should properly solve a problem I have using FileDialog in a Windows application I am writing...

Here is the relevant part of my code so far...

def SelectLogs(self, event):

    dir = os.getcwd()
    paths = []

    open_dlg = wx.FileDialog(self, message='Select log file',
        defaultDir=dir, style=wx.OPEN | wx.CHANGE_DIR | wx.MULTIPLE)

    self.splitter.Refresh(True)

    if open_dlg.ShowModal() == wx.ID_OK:
        paths = open_dlg.GetPaths()
        open_dlg.Destroy()

        self.splitter.Refresh()

        self.RetrieveLogData(paths)

    def RetrieveLogData(self, paths):

        count = 0
        loglines = []

        self.tc2.WriteText('Loading selected logs...')

        paths.sort()

        for log in paths:
            read_data = []
            f = open(log, 'r')

            self.tc2.WriteText('\r\n    ' + log)

            read_data = f.readlines()
            f.close()

            count = count + 1

            for line in read_data:
                loglines.append(line)
                self.tc1.WriteText(line)

        self.tc2.WriteText('\r\nCompleted loading ' + str(count) + ' log(s)')

My problem is that even though I call open_dlg.Destroy() and self.splitter.Refresh() I still have remnants of the FileDialog showing over the lower one of my wx.TextCtrl (tc2) objects (tc1 is in the top half of the splitter and tc2 in the bottom half).

Once the file(s) are fully loaded and contents displayed in tc1 then tc2 refreshes and displays as I would expect.

I have tried calling the refresh methods on tc1 and tc2 instead of on the splitter but this doesn't seem to have any effect.

Any useful suggestions will be welcome. Thanks.

役に立ちましたか?

解決

You do not need any calls to Refresh() and those will not work. The reason why your GUI freezes is different. Your callback function runs for too long and blocks your GUI thread.

Your should never run any task which may take more than just a few milliseconds in a callback function without creating a new thread.

You should create a new thread for your long running task. I recommend using wx.lib.delayedresult and wx.CallAfter as these are the most convenient. There are many questions / answers on this topic on SO. For example: How to implement a thread in a wxPython GUI application

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top