gpt4 book ai didi

python - python wx中打开子窗口时关闭父窗口

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:38 27 4
gpt4 key购买 nike

我编写了一段代码来打开文件选择器并选择 html 文件。当用户选择一个 html 文件时,它将显示在 Web 查看器中,但当 html 页面显示在 Web View 中时,我需要关闭之前用于打开文件选择器的窗口。

我尝试在网页显示在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)

另一方面,如果不关闭依赖于主框架的其他框架,则无法关闭主框架。但您可以隐藏()它。要再次显示它,只需调用 myframe.Show()

祝你好运。

关于python - python wx中打开子窗口时关闭父窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57536844/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com