【问题标题】:wxPython automatically does event at startup when it shouldn'twxPython 会在启动时自动执行不应该发生的事件
【发布时间】: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


    【解决方案1】:

    您在您认为绑定它的同一行上调用该方法。不是传递方法,而是传递被调用方法的返回值。

    在这一行中,第二个参数是一个方法:

    bindMethod(new, self.newFile)
    

    在这一行中,方法被调用,第二个参数是返回的,例如无:

    bindMethod(save, self.saveFile(self, self.f1)) 
    

    当你绑定方法的时候,参数并没有用到,所以你绑定到方法名并且不在那个地方调用它。当事件触发时,wxPython 应该使用正确的参数调用绑定的方法。

    这也可能意味着您不能使用已有的参数,因为给定了wx.EVT_MENU 的回调。您需要创建某种文件对话框以在 saveFile 方法中获取这些参数。

    【讨论】:

    • 所以基本上,当我调用绑定时,我不能调用参数,因为那实际上是在做方法,而不是绑定它?那么你的意思是,我必须找到一些在saveFile 方法中选择self.f1 文件的方法?
    • @SashaAhrestani 完全正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多