我写了一段代码来打开一个文件选择器并选择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方法中被关闭。