Hi. I just got started using wxPython and I was wondering how to create a button which transferred the user from one screen to another.

To kind of illustrate what I mean, I created two files, 'main.py' and 'newgame.py'. I've made a button called 'New Game' in the main.py file, and I'm trying to create a scenario where clicking this button will bring you to the 'newgame.py' screen.

What I've got for the 'main.py' file so far is:

import wx

class velvet(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Example', size = (800, 640))
        panel = wx.Panel(self)
        ngbutton = wx.Button(panel, label = "New Game", pos = (50, 50), size = (120, 120))
        self.Bind(wx.EVT_BUTTON, self.newgamebutton, ngbutton)
        exitbutton = wx.Button(panel, label = "Exit", pos = (50, 250), size = (120, 120))
        self.Bind(wx.EVT_BUTTON, self.closebutton, exitbutton)
        self.Bind(wx.EVT_CLOSE, self.closewindow)

    def newgamebutton(self, event):
        

    def closebutton(self, event):
        self.Close(True)

    def closewindow(self, event):
        self.Destroy()
        
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = velvet(parent = None, id = -1)
    frame.Show()
    app.MainLoop()

I was thinking that entering a self.event command for newgamebutton would solve the problem, but I'm not sure which event I could use or how I'd even get it to recognise that it should display the newgame.py file.

I've probably went about this in entirely the wrong way, but any help will be very much appreciated!

You can use Hide and Show, here is an example ...

# create two frames with wxFrame, only one is showing

import wx

class Frame1(wx.Frame):
    def __init__(self, parent, mytitle):
        wx.Frame.__init__(self, parent, -1, mytitle)
        self.SetBackgroundColour("green")
        # pass frame1 to frame2 as instance self
        self.frame2 = Frame2(None, 'Frame2', self)

        # create input widgets
        self.button1 = wx.Button(self, -1, label='show Frame2')
        # bind mouse event to an action
        self.button1.Bind(wx.EVT_BUTTON, self.button1Click)

        # use a box sizer to lay out widgets
        sizer_v = wx.BoxSizer(wx.VERTICAL)
        # Add(widget, proportion, flag, border)
        sizer_v.Add(self.button1, 0, flag=wx.ALL, border=50)
        self.SetSizer(sizer_v)
        # size the frame so all the widgets fit
        self.Fit()

    def button1Click(self, event):
        """button1 has been left clicked"""
        # self is instance of frame1
        self.Hide()
        self.frame2.Show()


class Frame2(wx.Frame):
    def __init__(self, parent, mytitle, frame1):
        wx.Frame.__init__(self, parent, -1, mytitle)
        self.SetBackgroundColour("brown")
        self.frame1 = frame1

        # create input widgets
        self.button1 = wx.Button(self, -1, label='show Frame1')
        # bind mouse event to an action
        self.button1.Bind(wx.EVT_BUTTON, self.button1Click)
        # optional respond to exit symbol x on frame2 title bar
        #self.Bind(wx.EVT_CLOSE, self.button1Click)

        # use a box sizer to lay out widgets
        sizer_v = wx.BoxSizer(wx.VERTICAL)
        # Add(widget, proportion, flag, border)
        sizer_v.Add(self.button1, 0, flag=wx.ALL, border=50)
        self.SetSizer(sizer_v)
        # size the frame so all the widgets fit
        self.Fit()

    def button1Click(self, event):
        """button1 has been left clicked"""
        # self is instance of frame2
        self.Hide()
        self.frame1.Show()


app = wx.App(0)
# create a MyFrame instance and show the frame
frame1 = Frame1(None, 'Frame1')
frame1.Show()
app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.