在wx python中打开一个子窗口时关闭父窗口

1 人关注

我写了一段代码来打开一个文件选择器并选择html文件。当用户选择一个html文件时,它将显示在一个网络浏览器中,但当html页面显示在网络视图中时,我需要关闭之前用于打开文件选择器的窗口。

我试着在webview上显示完网页后让FileChooserframe.Close()。

import os
import wx
import wx.html2
import facial_expression_recognition_from_stream
import csvReader
FileFilter = "Html files (*.html)|*.html|" \
             "All files (*.*)|*.*"
class UXEvaluationApp(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Choose a html file")
        panel = wx.Panel(self, wx.ID_ANY)
        self.currentDirectory = os.getcwd()
        openFileDlgBtn = wx.Button(panel, label="Choose a HTML File")
        openFileDlgBtn.Bind(wx.EVT_BUTTON, self.onOpenFile)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(openFileDlgBtn, 0, wx.ALL | wx.CENTER, 5)
        panel.SetSizer(sizer)
    def onOpenFile(self, event):
        dlg = wx.FileDialog(self, message="Choose a html file", defaultDir=self.currentDirectory, defaultFile="", wildcard=FileFilter, style=wx.FD_OPEN | wx.FD_CHANGE_DIR)
        if dlg.ShowModal() == wx.ID_OK:
            htmlFilePath = dlg.GetPath()
            FileChooserframe.Close()
            myBrowserInstance = MyBrowser(None, -1)
            myBrowserInstance.browser.LoadURL(htmlFilePath)
            myBrowserInstance.Show()
            facial_expression_recognition_from_stream.main()
            FileChooserframe.Close()
        dlg.Destroy()
class MyBrowser(wx.Dialog):
    def __init__(self, *args, **kwds):
        wx.Dialog.__init__(self, *args, **kwds)
        sizer = wx.BoxSizer(wx.VERTICAL)
        button = wx.Button(self, label="Complete Tracking", pos=(20, 70))
        self.browser = wx.html2.WebView.New(self)
        sizer.Add(button, 0, wx.EXPAND, 10)
        sizer.Add(self.browser, 1, wx.EXPAND, 10)
        self.SetSizer(sizer)
        button.Bind(wx.EVT_BUTTON, self.getReport)
        self.SetSize((700, 700))
    def getReport(self, event):
        facial_expression_recognition_from_stream.stop()
        frame = Report()
        frame.Show()
class Report(wx.Panel):
    def __init__(self):
        csvReader.showReport()
if __name__ == "__main__":
    app = wx.App(False)
    FileChooserframe = UXEvaluationApp()
    FileChooserframe.Show()
    app.MainLoop()

我需要FileChooserFrame在onOpenFile方法中被关闭。

1 个评论
Close() 应该可以。你有没有试过 Destroy()
python
wxpython
user3789211
user3789211
发布于 2019-08-17
1 个回答
Dan A.S.
Dan A.S.
发布于 2019-10-01
已采纳
0 人赞同

你可以使用一个文件选择器,一旦用户选择了一个文件就会关闭。

From: https://wxpython.org/Phoenix/docs/html/wx.FileDialog.html

with wx.FileDialog(self, "Open XYZ file", wildcard="XYZ files (*.xyz)|*.xyz",
                       style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
        if fileDialog.ShowModal() == wx.ID_CANCEL:
            return     # the user changed their mind
        # Proceed loading the file chosen by the user
        pathname = fileDialog.GetPath()
            with open(pathname, 'r') as file:
                self.doLoadDataOrWhatever(file)
        except IOError:
            wx.LogError("Cannot open file '%s'." % newfile)