最小 wx.FileDialog 示例冻结程序

Minimal wx.FileDialog example freezes program

我正在编写一个新的应用程序。

几年前我曾经使用 python 2.x 和 wxPython,现在我得到了 Python 3.7.0 和 wxPythonPhoenix 4.0.4 msw(唉)。

当我尝试将 wx.FileDialog 与 ShowModal 一起使用时,程序冻结。

我没有找到任何关于此的问题。 我使用(并减少了 MWE)来自 wxWiki 的代码,如 this.

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title, size=(400, 200))
        fileMenu = wx.Menu()
        openItem = fileMenu.Append(-1, "&Open...\tCtrl-O", "Open a new recipe")
        menuBar = wx.MenuBar()
        menuBar.Append(fileMenu, "&File")
        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU, self.OnOpen, openItem)
    def OnOpen(self, event):
        # otherwise ask the user what new file to open
        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

        #you'll never get here      

app = wx.App()
frame = MyFrame(None, -1, "test")
frame.Show()
app.MainLoop()

我真的不明白,而且代码实际上工作了几次。

我从我从 wxpython wiki 复制的最小工作示例开始。

然后我将其简化为您的最小工作示例。我认为问题可能是 fileMenu.Append(-1, ….

的 id 的 -1

最小最小工作示例 (MWE):

import wx                                                                                                                           

app = wx.App()
with wx.FileDialog(None, "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:
        print("the user changed their mind")
    else:
        # Proceed loading the file chosen by the user
        pathname = fileDialog.GetPath()
        try:
            with open(pathname, 'r') as file:
                print(file.read())
        except IOError:
            wx.LogError("Cannot open file '%s'." % newfile)

根据您的代码,完美运行(始终):

import wx                                                                                                                           

class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title, size=(400, 200))
        fileMenu = wx.Menu()
        openItem = fileMenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")
        menuBar = wx.MenuBar()
        menuBar.Append(fileMenu, "&File")
        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU, self.OnOpen, openItem)
    def OnOpen(self, event):
        # otherwise ask the user what new file to open
        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)
                    print(file.read()) # see, it works!
            except IOError:
                wx.LogError("Cannot open file '%s'." % newfile)


app = wx.App()
frame = MyFrame(None, wx.ID_ANY, "test")
frame.Show()
app.MainLoop()

更漂亮,有一些解释:

import wx                                                                                                                           

class MainWindow(wx.Frame):
    def __init__(self, parent, title):

        # A "-1" in the size parameter instructs wxWidgets to use the default size.
        # In this case, we select 200px width and the default height.
        wx.Frame.__init__(self, parent, title=title, size=(200,-1))

        # Setting up the menu.
        filemenu= wx.Menu()
        menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")

        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.

        # Events.
        self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)

        self.Show()

    def OnOpen(self,e):
        """ Open a file"""
        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)
                    print(file.read())
            except IOError:
                wx.LogError("Cannot open file '%s'." % newfile)


app = wx.App(False)
frame = MainWindow(None, "test")
app.MainLoop()

另见

文件对话框的更多 GUI 选项:JFileChooser for Python?