0

Ok, so I am playing around and trying to get an understanding of how to make GUI's with buttons, I figured i would start simple and make one with two buttons that displays a different message depending on which one is clicked. I made the first button and tested it... worked fine, made the second button and when i tested it i get the message for the second button when i click the first button, and nothing when i click the second button. I tried searching around but it doesn't seem anyone else had this issue so I am obviously doing something wrong.

#!/usr/bin/env python
import os
import wx

class Frame(wx.Frame):

    def OnOpen(self,e):
        self.dirname=''
        dlg=wx.FileDialog(self,'Choose a File',self.dirname,'','*.*',wx.OPEN)

        if dlg.ShowModal()==wx.OK:
            self.filename=dlg.GetFileName()
            self.dirname=dlg.GetDirectory()
            f=open(os.path.join(self.dirname,self.filename),'r')
            self.Control.SetValue(f.read())
            f.close()
            dlg.Destroy()

    def OnAbout(self,e):
        dlg=wx.MessageDialog(self,'Aoxx','Author',wx.OK)
        dlg.ShowModal()
        dlg.Destroy()

    def OnExit(self,e):
        dlg=wx.MessageDialog(self,'Exit','Terminate',wx.OK)
        dlg.ShowModal()
        self.Close(True)
        dlg.Destroy()

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Frame works',size=(450,600))
        panel=wx.Panel(self)

        self.CreateStatusBar()

        filemenu=wx.Menu()
        self.filemenu=wx.Menu()
        menubar=wx.MenuBar()
        menubar.Append(filemenu,'&File')
        #menubar.Append(filemenu,'&Help')
        self.SetMenuBar(menubar)

        MenuOpen=filemenu.Append(wx.ID_OPEN,'&Open','File Dir')
        MenuExit=filemenu.Append(wx.ID_ANY,'E&xit','Term')
        MenuAbout=filemenu.Append(wx.ID_ABOUT,'&About','Info')

        self.Bind(wx.EVT_MENU,self.OnOpen,MenuOpen)
        self.Bind(wx.EVT_MENU,self.OnExit,MenuExit)
        self.Bind(wx.EVT_MENU,self.OnAbout,MenuAbout)



        pic1=wx.Image('C:\Users\******\Pictures\Tri.bmp', wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        self.button=wx.BitmapButton(panel,-1, pic1,pos=(10,10))
        self.Bind(wx.EVT_BUTTON,self.ClickTri,self.button)
        self.button.SetDefault()

        pic2=wx.Image('C:\Users\******\Pictures\ClickWin.bmp', wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        self.buton=wx.BitmapButton(panel,-1,pic2,pos=(220,10))
        self.Bind(wx.EVT_BUTTON,self.ClickWin,self.button)


    def ClickTri(self,event):
        dlg=wx.MessageDialog(self,'No touching the TriForce Rook!','HEY!!!',wx.OK)
        dlg.ShowModal()
        dlg.Destroy()       

    def ClickWin(self,event):
        dlg=wx.MessageDialog(self,'You would.....','REALLY?',wx.OK)
        dlg.ShowModal()
        dlg.Destroy()

        self.Show(True)

if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=Frame(None,id=-1)
    frame.Show()
    app.MainLoop()

1 Answer 1

1

you cant have 2 self.button make the second one self.button2 or something

Sign up to request clarification or add additional context in comments.

2 Comments

If i wanted the button to open a new frame would i need to define another class under that one? exp def ClickTri(self,event): class Frame2(wx.Frame): ....
it depends what you wanted ... you would probably need a new frame defined somewhere ... although not in your click event you would do def OnClick(self,evt):f2 = SomeOtherFrame();f2.Show()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.