【发布时间】:2016-02-16 06:06:39
【问题描述】:
所以,我对编程很陌生。我目前正在 Mac 上的 wxPython 中制作一个简单的文本编辑器(claaaassic)。我有一个绑定到事件 saveFile() 的“保存”菜单项,它采用自定义类 file() 作为指定文件的目录和文件类型的参数。一切看起来都很正常,但由于某种原因,该方法在启动时自动调用,并且在我实际单击“保存”菜单项时没有运行。任何帮助将不胜感激!
另外,下面的代码只是我的程序的一个示例。
import wx
# Custom class file
class file():
def __init__(self, directory, file_type):
self.directory = directory #location of file
self.file_type = file_type #type of file (text, HTML, etc.)
# Main Frame
class Frame1(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, None, -1, 'Text Program', size=(500, 700))
self.Bind(wx.EVT_CLOSE, self.Exit)
self.f1 = file("/Users/Sasha/Desktop/File.txt", "Text File") # an example file
self.InitUI()
def InitUI(self):
self.text = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.CreateStatusBar()
# the reason we use "self.text" instead of just "text", is because if we want to use multiple windows, I believe
self.menu()
# Last. Centers and shows the window.
self.Centre()
self.Show()
# Save file, WHERE I HAVE PROBLEMS
def saveFile(self, a, file):
directory = file.directory
file_type = file.file_type
file = open(directory, "r+") #r+ is reading and writing, so if file is same, no need to write
print file.name + ",", file_type
l = file.read()
print l
file.close()
# Exit method, pops up with dialog to confirm quit
def Exit(self, a):
b = wx.MessageDialog(self, "Do you really want to close this application?", 'Confirm Exit', wx.CANCEL | wx.OK)
result = b.ShowModal()
if result == wx.ID_OK:
self.Destroy()
b.Destroy()
def menu(self):
# A shortcut method to bind a menu item to a method
def bindMethod(item, method):
self.Bind(wx.EVT_MENU, method, item)
# FILE
fileMenu = wx.Menu()
new = fileMenu.Append(wx.ID_NEW, "New")
save = fileMenu.Append(wx.ID_SAVE, "Save")
saveAs = fileMenu.Append(wx.ID_SAVEAS, "Save As")
bindMethod(new, self.newFile)
bindMethod(save, self.saveFile(self, self.f1)) # WHERE I MAY HAVE PROBLEMS
bindMethod(saveAs, self.saveAs)
menuBar = wx.MenuBar()
menuBar.Append(fileMenu, "&File") # Adding the "filemenu" to the MenuBar
self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
if __name__ == '__main__':
app = wx.App()
frame = Frame1()
app.MainLoop()
【问题讨论】:
标签: python user-interface events wxpython