Getting Button Click to trigger method in Backend

Hi Everyone,

I am currently working on a GUI in wxglade (using wxpython of course), and I am trying to work out the best way to get it so that when a button is clicked, it triggers something in another python file. Basically the following

gui.py
import wx
#all the wx python code
#upon a button click, runs clicked() in main.py

main.py
import gui.py

start GUI

def clicked():
#runs when button is clicked.

Also, is it possible to then update the GUI from main.py based on the output of the function. I have been trying to work this out for ages -> from a simple example of how to do this I should be able to work out what to do from then on. I want to keep the backend and GUI sepearte so it is really easy to alter the GUI.

Thanks for any help!

  • Josh

Joshua Henderson
+61 449 128 074
joshhendo@gmail.com

import main.py

in your GUI code. Instantiate the necessary objects, then call the
required backend functionality in the event handlers for the button or
menu or choice or whatever widget.

very pseudo code guaranteed not to run...

import backend

class MyFrame(wx.Frame):
    def __init__(self):
        self.btn = wx.Button(self, label="DoSomething")
        self.btn.Bind(wx.EVT_BUTTON, self.do_something)

    def do_something(self, event):
        wx.GetApp().be.method_to_do_something()

class MyApp(wx.App):
    def OnInit(self):

       self.be = backend.core_class()

        self.frame = MyFrame(None, title="Frontend for my backend")
        self.frame.Show(True)
        self.SetTopWindow(self.frame)

···

On Tue, Aug 5, 2008 at 5:12 AM, Joshua Henderson <joshhendo@gmail.com> wrote:

Hi Everyone,

I am currently working on a GUI in wxglade (using wxpython of course), and I
am trying to work out the best way to get it so that when a button is
clicked, it triggers something in another python file. Basically the
following

gui.py
  import wx
  #all the wx python code
  #upon a button click, runs clicked() in main.py

main.py
  import gui.py
  # start GUI
  def clicked():
    #runs when button is clicked.

Also, is it possible to then update the GUI from main.py based on the output
of the function. I have been trying to work this out for ages -> from a
simple example of how to do this I should be able to work out what to do
from then on. I want to keep the backend and GUI sepearte so it is really
easy to alter the GUI.

Thanks for any help!

- Josh

--
Stand Fast,
tjg. [Timothy Grant]

Joshua Henderson wrote:

Hi Everyone,

I am currently working on a GUI in wxglade (using wxpython of course), and I am trying to work out the best way to get it so that when a button is clicked, it triggers something in another python file. Basically the following

gui.py
  import wx
  #all the wx python code
  #upon a button click, runs clicked() in main.py

main.py
  import gui.py
  # start GUI
  def clicked():
    #runs when button is clicked.

Also, is it possible to then update the GUI from main.py based on the output of the function. I have been trying to work this out for ages -> from a simple example of how to do this I should be able to work out what to do from then on. I want to keep the backend and GUI sepearte so it is really easy to alter the GUI.

Thanks for any help!

- Josh

Joshua Henderson
+61 449 128 074
joshhendo@gmail.com <mailto:joshhendo@gmail.com>

The pubsub module is practically designed for this very purpose. One other way to do this is something like this (some code snitched from Timothy)

<code>

#gui.py
from someFile import myHandler

self.btn = wx.Button(self, label="DoSomething")
self.btn.Bind(wx.EVT_BUTTON, myHandler)

</code>

The way it currently looks in your example could cause a recursive import. So don't do that!

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org