1

i use wxpython for python3

i try to creat my simple application and i 'm try to create the menu and with 2 panels i need to add a file dialog when the user click in the button OPEN in the menu to chose the file , i don't know how can add it in my code

this is my code :

import wx
class MainFrame(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(None, *args, **kwargs)
        self.Title = 'premier app (Menu+2windows)'
        self.SetMenuBar(MenuBar(self))
        self.ToolBar = MainToolbar(self)
        self.status_bar = StatusBar(self).status_bar
        self.Bind(wx.EVT_CLOSE, self.on_quit_click)
        panel = MainPanel(self)
        sizer = wx.BoxSizer()
        sizer.Add(panel)
        self.SetSizerAndFit(sizer)
        self.Centre()
        self.Show()

    def on_quit_click(self, event):

        del event
        wx.CallAfter(self.Destroy)


class MainPanel(wx.Panel):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent,size = (500,500))
        self.splitter = wx.SplitterWindow(self, -1, size = (500,500))

# 1er panel 
        pan1 = wx.Window(self.splitter, style=wx.BORDER_SUNKEN)
        pan1.SetBackgroundColour("yellow")
        wx.StaticText(pan1, -1)


#2em panel
        pan2 = wx.Window(self.splitter, style=wx.BORDER_SUNKEN)
        pan2.SetBackgroundColour("blue")
        wx.StaticText(pan2, -1)
        self.splitter.SplitVertically(pan1, pan2, 100)


class MenuBar(wx.MenuBar):
    """creation de menu."""
    def __init__(self, parent, *args, **kwargs):
        super(MenuBar, self).__init__(*args, **kwargs)
        #  menu
        File_menu = wx.Menu()
        Edit_menu = wx.Menu()
        Help_menu = wx.Menu()

        self.Append(File_menu, '&File')
        self.Append(Edit_menu, '&Edit')
        self.Append( Help_menu, '&Help')

        quit_menu_item = wx.MenuItem(File_menu, wx.ID_EXIT)
        parent.Bind(wx.EVT_MENU, parent.on_quit_click, id=wx.ID_EXIT)
        open_menu_item = wx.MenuItem(File_menu, wx.ID_OPEN)
        new_menu_item = wx.MenuItem(File_menu,wx.ID_NEW)

        File_menu.Append(open_menu_item)
        File_menu.Append(new_menu_item)
        File_menu.Append(quit_menu_item)


class MainToolbar(wx.ToolBar):
    """creation toolbar."""
    def __init__(self, parent, *args, **kwargs):
        super(MainToolbar, self).__init__(parent, *args, **kwargs)



class StatusBar(object):
    def __init__(self, parent):
        self.status_bar = parent.CreateStatusBar()


if __name__ == '__main__':
    """Run the application."""
    screen_app = wx.App()
    main_frame = MainFrame()
    screen_app.MainLoop()

need some help thank u

6
  • Did you take a look on this page: wxpython.org/Phoenix/docs/html/wx.FileDialog.html ? Commented Feb 27, 2019 at 12:51
  • yes , but is not easy i can't understand how can i add the line of code in my code exactely ? thanks Commented Feb 27, 2019 at 13:40
  • K-Doe when i add the line def OnOpen(self, event): if self.contentNotSaved:.... wx.LogError("Cannot open file '%s'." % newfile) nothing happening Commented Feb 27, 2019 at 13:59
  • Did you bind your open_menu_item to OnOpen ? Commented Feb 27, 2019 at 14:02
  • no ! how can i do it ! Commented Feb 27, 2019 at 14:31

1 Answer 1

1

With this code:

quit_menu_item = wx.MenuItem(File_menu, wx.ID_EXIT)
parent.Bind(wx.EVT_MENU, parent.on_quit_click, id=wx.ID_EXIT)

you are binding the Quit menu item to a Quit function.
Do the same for the Open function i.e. Bind it to something like parent.on_open_file

In that function, do something like this.

def self.on_open_file(self, event):
    dlg = wx.FileDialog(self, message="Choose file", defaultDir = "/home", defaultFile = "",\
                        wildcard = "", style=wx.FD_OPEN)
    if dlg.ShowModal() == wx.ID_OK:
        print(dlg.GetDirectory(), dlg.GetFilename())
    dlg.Destroy()
Sign up to request clarification or add additional context in comments.

3 Comments

i do that but i still have some problem like has no attribut on_open_file
You have to be consistent. With parent.Bind(wx.EVT_MENU, parent.on_open_file,id=wx.ID_OPEN) you have to put the function in the parent i.e. MainFrame as you did with on_click_quit OR declare the bind as parent.Bind(wx.EVT_MENU, self.onOpen,id=wx.ID_OPEN) because you wrote it as onOpen. While you're at it change Affichage_menu back to View_menu as it introduces an error.
when i do .def self.on_open_file(self, event): i had error like " invalid syntex "

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.