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

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
Sarah Tohami
  • 59
  • 1
  • 12

1 Answers1

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()
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • i do that but i still have some problem like has no attribut on_open_file – Sarah Tohami Feb 28 '19 at 08:15
  • 1
    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. – Rolf of Saxony Feb 28 '19 at 09:19
  • when i do `.def self.on_open_file(self, event):` i had error like " invalid syntex " – Sarah Tohami Feb 28 '19 at 14:19