在 wx python 中打开子 window 时关闭父 window

Closing the parent window when opening a child window in wx python

我编写了一个代码来打开文件选择器和 select html 个文件。当用户 select 有一个 html 文件时,它将显示在网络查看器中,但我需要关闭之前的 window,我在 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()

我需要在 onOpenFile 方法上关闭 FileChooserFrame

您可以使用在用户选择文件后关闭的文件选择器:

发件人: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()
        try:
            with open(pathname, 'r') as file:
                self.doLoadDataOrWhatever(file)
        except IOError:
            wx.LogError("Cannot open file '%s'." % newfile)

另一方面,您不能在不关闭依赖于它的其他框架的情况下关闭主框架。但是你可以 Hide() 它。要再次显示它,只需调用 myframe.Show()

祝你好运。