我正在编写一个新的应用程序。
几年前,我曾经使用过python 2.x和wxPython,现在我有了Python 3.7.0和wxPythonPhoenix 4.0.4 msw(alas)。
当我尝试将wx.FileDialog与ShowModal一起使用时,程序将冻结。
我以前没有发现有关此的任何问题。 我使用了this之类的wxWiki中的代码(对于MWE来说是简化的)。
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()
我真的不明白,而且代码实际上工作了几次。
答案 0 :(得分:1)
我从从wxpython wiki复制的最小工作示例开始。
然后我将其简化为您的最小工作示例。我认为问题可能出在<!DOCTYPE html>
<html>
<head>
<style>
[contenteditable=true] {
position: relative;
}
[contenteditable=true]:empty:before {
content: attr(placeholder);
position: absolute;
left: 0;
right: 0;
top: 4px;
margin: auto;
}
#myDiv {
border: 1px dashed #AAA;
width: 290px;
padding: 5px;
text-align: left;
height: 16px;
}
</style>
</head>
<body>
<div id="myDiv" contenteditable="true" placeholder="Enter text here..."></div>
</body>
</html>
的ID的-1
值上。
fileMenu.Append(-1, …
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()
文件对话框的更多GUI选项:JFileChooser for Python?