Search code examples
pythonuser-interfacewxpythonwxwidgets

wxPython - Creating a button that produces a bitmap image within my TextCtrl panel


import wx

class bucky(wx.Frame):

    # Creating the outer window/frame
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Karls Network Tool', size=(900,700))
        panel=wx.Panel(self, -1)

        # Exit Button
        button=wx.Button(panel,label="Exit",pos=(840,580),size=(40,40))

        # Close event for exit button
        self.Bind(wx.EVT_BUTTON, self.closebutton, button)
        self.Bind(wx.EVT_CLOSE, self.closewindow)

        # Network Topology Area - User can map a network
        wx.TextCtrl(panel, -1, pos=(10,40), size=(525, 400)) # Network topology Panel

        # Creating the bitmap buttons - Images appear on buttons 
        buttonOneRouter=wx.Image("router.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        self.buttonRouter=wx.BitmapButton(panel, -1, buttonOneRouter, pos=(20,580))

Ok so I have my Frame working as I would expect set at the size I need, I also have my exit button which works as expected etc etc..... So as we can see the general program is working fine. All I want todo now is have the program do the following:

  • When a user clicks on the buttonOneRouter which as you would expect is a button within my frame with a picture of a router on. The router image that is used on the button and stored in my Python directory should appear in the TextCtrl - network topology area.

-Taking this a step forward, if its possible I would then want to be able to drag this small image which has appeard in the TextCtrl within this area so the user can create their simulated network.

I understand that this should be pretty simple, as all I have to really do is create an event to assign to the button, but I dont no were to even begin. Any pointers?


Solution

  • Here is what I think you should do:

    write init method which also takes image as your argument that you will be using for for textctrl background.

    In your case, it will be:

    def __init__(self,parent, id, image)
    

    Create your own app class and create image object on you OnInit method:

    def OnInit(self):
    
       image=wx.Image('your_image.jpg',wx.BITMAP_TYPE_JPEG)
       self.frame=bucky(image)
    

    Rest of the process are as same as you do other things.