Sarah Tohami
Sarah Tohami

Reputation: 59

add file dialog to Menu Functionality (open ) wxpython 3

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

Upvotes: 1

Views: 522

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22433

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()

Upvotes: 1

Related Questions