Button event question Topic is solved

This forum is reserved for everything you want to talk about. It could be about programming, opinions, open source programs, development in general, or just cool stuff to share!
Post Reply
ojibway
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Mar 07, 2011 9:54 pm
Location: Calgary, Alberta, Canada

Button event question

Post by ojibway »

I am making a wxprogram to show the geometry associated to elements on the periodic table. The image of a periodic table is my opening frame page with each rectangular image being a button(112 buttons). What I want to do is have the button click event open another window that will contain a GLcanvas. This would allow the user to have additional windows open so that different elements and thier geometry can be compared. I also want to have the glcanvas frame on a separate file, that can be accessed by the on click event. I have been looking for code for this purpose but cannot find it. Any help would be appreciated, a newbie to wx programming, I am.
"Advances are made by answering questions, Discoveries are made by questioning answers"
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Sorry, I'm not sure I understand where the problem is. Start by reading tutorials on how to catch events
"Keyboard not detected. Press F1 to continue"
-- Windows
ojibway
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Mar 07, 2011 9:54 pm
Location: Calgary, Alberta, Canada

Post by ojibway »

What I want my program to do is leave the mainpage accessible at all times, which is basically a page containing nothing more than buttons. When a button is pressed I do not want to repaint(or what ever call it) the frame, but instead open another frame in its own window. That window will contain the GLcanvas.
Last edited by ojibway on Tue Mar 08, 2011 10:31 am, edited 1 time in total.
"Advances are made by answering questions, Discoveries are made by questioning answers"
briceandre
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 672
Joined: Tue Aug 31, 2010 6:22 am
Location: Belgium

Post by briceandre »

In the event handler of your button, you simply create a new frame. Both frames (the one with your button and the new one) will be displayed and the user will be able to interact with both.

So, you create a new frame (SubFrame, for example), you connect a handler to your button (OnButton) and you simply write this :

Code: Select all

void OnButton(wxCommandEvent& e)
{
   new SubFrame(...);
}
Your program will exit when both frames will be closed.
ojibway
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Mar 07, 2011 9:54 pm
Location: Calgary, Alberta, Canada

Post by ojibway »

Here is my code so far, truncated to only include 4 buttons instead of the 112. Should I be learning a different language before the code you see? I also noticed another problem, with 4 buttons the loading of the program is fast, but with 112 buttons the white space behind the buttons is built before the images are loaded. It loads fine, but I would like to get rid of the visual of that white space. Thanks for the help, by the way.

Edited:Hmmm, this is C++ programming forum, and here I am programming in wxpython. Which one works better as a cross platform across the various OS's?

Code: Select all

#!/usr/bin/python

# statusbar.py

import wx

class Statusbar(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(800, 640), 
	style=wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX)

        panel = wx.Panel(self, 1)
        self.SetBackgroundColour(wx.BLACK)
        self.Centre()
        self.sb = self.CreateStatusBar()
              
        ## set-up for sizers
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        
        ## adding atom buttons row #1
        h = wx.BitmapButton(panel, 2, wx.Bitmap('images/hydrogen.jpg'))
        he = wx.BitmapButton(panel, 3, wx.Bitmap('images/helium.jpg'))
        # row #2
        li = wx.BitmapButton(panel, 4, wx.Bitmap('images/lithium.jpg'))
        be = wx.BitmapButton(panel, 5, wx.Bitmap('images/beryllium.jpg'))

        ## assigining id's to sizer
        hbox1.Add(h), hbox1.Add((0, -1), 1, flag=wx.EXPAND | wx.ALIGN_RIGHT), hbox1.Add(he)
        hbox2.Add(li), hbox2.Add((0, -1), 1, flag=wx.EXPAND | wx.ALIGN_RIGHT), hbox2.Add(be)

        ## event handler for statusbar input
        panel.Bind(wx.EVT_ENTER_WINDOW, self.EnterPanel, id=1)
        h.Bind(wx.EVT_ENTER_WINDOW, self.EnterH, id=2)
        he.Bind(wx.EVT_ENTER_WINDOW, self.EnterHe, id=3)
        li.Bind(wx.EVT_ENTER_WINDOW, self.EnterLi, id=4)
        be.Bind(wx.EVT_ENTER_WINDOW, self.EnterBe, id=5)

        vbox.Add(hbox1, 1, wx.EXPAND)
        vbox.Add(hbox2, 1, wx.EXPAND)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(vbox, flag=wx.EXPAND | wx.RIGHT | wx.LEFT | wx.BOTTOM | wx.TOP, border=10)

        self.SetSizer(sizer)
        self.Show(True)
        
        ## definitions for statusbar input
    def EnterPanel(self, event):
        self.sb.SetStatusText('Click Element to View The Geometry')
        event.Skip()
    def EnterH(self, event):
        self.sb.SetStatusText('Hydrogen Atom, Click to View Geometry')
        event.Skip()
    def EnterHe(self, event):
        self.sb.SetStatusText('Helium Atom, Click to View Geometry')
        event.Skip()
    def EnterLi(self, event):
        self.sb.SetStatusText('Lithium Atom, Click to View Geometry')
        event.Skip()
    def EnterBe(self, event):
        self.sb.SetStatusText('Beryllium Atom, Click to View Geometry')
        event.Skip()
        
class MyApp(wx.App):
    def OnInit(self):
        frame = Statusbar(None, -1, 'Test')
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

app = MyApp(0)
app.MainLoop()
EDIT by Auria: Please use code tags
"Advances are made by answering questions, Discoveries are made by questioning answers"
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Regarding the white space, I'm not sure what you mean, sorry

wxPython can be discussed here, though indeed the C++ version of wxWidgets is what is most discussed here; wxPython is most discussed on the mailing list.

Edited:Hmmm, this is C++ programming forum, and here I am programming in wxpython. Which one works better as a cross platform across the various OS's?
Which language? I don't think it makes much difference.
Under OSX and Windows the C++ version is perhaps a little easier to package, but then Python is generally easier to learn.
"Keyboard not detected. Press F1 to continue"
-- Windows
ojibway
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Mar 07, 2011 9:54 pm
Location: Calgary, Alberta, Canada

Post by ojibway »

Auria wrote:Regarding the white space, I'm not sure what you mean, sorry
In wxpython, bitmapbuttons are given a white space around them, but it might be something that comes from photoshop. All I know is if I give the same button a staticBitmap label instead, the white space, or background frame around the button disappears, because now the same image is not a button. But, when the program is loading it writes a white space of the buttons and any static bitmaps first, and then it loads the actual images. And because I have 112 buttons the loading process is quite slow, and the white space annoying. Maybe I can preload the images? I don't mind having the white back frame around the buttons, it looks alright.
"Advances are made by answering questions, Discoveries are made by questioning answers"
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Well indeed you can preload the images, just create all the wx.Bitmap object first (before the frame).

Another option is to call Freeze first, THEN when you're done call Thaw)
"Keyboard not detected. Press F1 to continue"
-- Windows
ojibway
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Mar 07, 2011 9:54 pm
Location: Calgary, Alberta, Canada

Post by ojibway »

After spending the last 12 hours of trying to get a glcanvas to open in a different window, I am not sure of the word "easier" as being an appropiate word for describing learning Python. But I finally got it to work, I am alot more bald though.
"Advances are made by answering questions, Discoveries are made by questioning answers"
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Really, the only cause I see that could make it take 12 hours, is if you're trying to do it without having studied the appropriate concepts first :)

Have you learned about object-oriented programming, etc.? Have you read wxWidgets tutorials?
"Keyboard not detected. Press F1 to continue"
-- Windows
ojibway
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Mar 07, 2011 9:54 pm
Location: Calgary, Alberta, Canada

Post by ojibway »

I should have a book of some sorts, I know, but I don't, cannot get one until the end of the month. So, all I have to go by is the code within the wxpython demo, which is not forth coming in explaining what the code is doing. Forums are on a short list, so can't get help there either. So, I am learning in a rather unorthodox way, alot of misses with getting the code to run, which means more time, but I will remember and understand how the code is working, the hard way. I had downloaded Avogardo, a molecule builder program, and wondered how it worked, I found a tutorial on showmedo of a guy who was building a similar program and followed along. So, I started playing around with my own code, only been at it for a week. At the moment, I am quite pleased with the results of my first program. It's a rather simply one actually, designed to show the geometry of atoms. My next project will be a molecule builder, that will be a bit more involved. But, I'll have a book for reference by then.
"Advances are made by answering questions, Discoveries are made by questioning answers"
Post Reply