How do I connect a submit button on my GUI to python script?

I created a GUI with some text boxes to fill in numbers and a submit
button. When users click the submit button, I want to replace the
variables in my python script by the numbers that users typed in and
execute the python codes.

Say for example, my codes help users calculate the quantiles of a
normal distribution with mean mu and standard deviation sigma. Users
will have to input the values that they want for mu and sigma (say mu
= 0 and sigma = 1). When they click the submit button, the GUI will
connect to my python codes and the codes will calculate quantiles of a
normal distribution with mean 0 and standard deviation 1.

Please help me out!

Thank you,
Janey

Janey wrote:

I created a GUI with some text boxes to fill in numbers and a submit
button. When users click the submit button, I want to replace the
variables in my python script by the numbers that users typed in and
execute the python codes.

Say for example, my codes help users calculate the quantiles of a
normal distribution with mean mu and standard deviation sigma. Users
will have to input the values that they want for mu and sigma (say mu
= 0 and sigma = 1). When they click the submit button, the GUI will
connect to my python codes and the codes will calculate quantiles of a
normal distribution with mean 0 and standard deviation 1.

Please help me out!

Thank you,
Janey

in the event handler for your GUI calculate button:

mu = int(self.muButton.GetValue())
sigma = int(self.sigmaButton.GetValue())

something = mu * sigma

When you create your text inputs, you'll have to name them
"self.[something]", "self.something_else" so that your event function
can read their values.

Hope that makes some sense

···

--
Steven Sproat, BSc

Janey wrote:

I created a GUI with some text boxes to fill in numbers and a submit

button. When users click the submit button, I want to replace the

variables in my python script by the numbers that users typed in and

execute the python codes.

Say for example, my codes help users calculate the quantiles of a

normal distribution with mean mu and standard deviation sigma. Users

will have to input the values that they want for mu and sigma (say mu

= 0 and sigma = 1). When they click the submit button, the GUI will

connect to my python codes and the codes will calculate quantiles of a

normal distribution with mean 0 and standard deviation 1.

Please help me out!

Thank you,

Janey

in the event handler for your GUI calculate button:

Do you know how to bind events to buttons?

mu = int(self.muButton.GetValue())

sigma = int(self.sigmaButton.GetValue())

I think you meant:

mu = int(self.muTextCtrl.GetValue())
sigma = int(self.sigmaTextCtrl.GetValue())

The OP, in learning wxPython, will find that the typical methods needed for getting or setting values of widgets are all in there. For a question like this, check the methods for textCtrl and you’ll find it. http://docs.wxwidgets.org/stable/wx_wxtextctrl.html (this is wxWidgets version, which I find easier on the eyes).

Che

···

On Sat, Jul 17, 2010 at 7:58 AM, Steven Sproat sproaty@gmail.com wrote:

C M wrote:

    Janey wrote:
    > I created a GUI with some text boxes to fill in numbers and a submit
    > button. When users click the submit button, I want to replace the
    > variables in my python script by the numbers that users typed in and
    > execute the python codes.
    >
    > Say for example, my codes help users calculate the quantiles of a
    > normal distribution with mean mu and standard deviation sigma. Users
    > will have to input the values that they want for mu and sigma
    (say mu
    > = 0 and sigma = 1). When they click the submit button, the GUI will
    > connect to my python codes and the codes will calculate
    quantiles of a
    > normal distribution with mean 0 and standard deviation 1.
    >
    > Please help me out!
    >
    > Thank you,
    > Janey
    >
    >
    in the event handler for your GUI calculate button:

Do you know how to bind events to buttons?

Uh-huh.

    mu = int(self.muButton.GetValue())
    sigma = int(self.sigmaButton.GetValue())

I think you meant:

mu = int(self.muTextCtrl.GetValue())
sigma = int(self.sigmaTextCtrl.GetValue())

<http://groups.google.com/group/wxPython-users?hl=en&gt;

yeah my bad I'd just woken up

···

On Sat, Jul 17, 2010 at 7:58 AM, Steven Sproat <sproaty@gmail.com > <mailto:sproaty@gmail.com>> wrote:

--
Steven Sproat, BSc

Do you know how to bind events to buttons?

Uh-huh.

Sorry, that was not meant for Steven but for Janey the OP, who I assumed was just learning. :smiley:

It was ambiguous the way I wrote it.

Che

Thank you all for your help! And yes, CM was right, I just learned wxpython for 3 days!

I actually have another question and hopefully you can help me out again here. My python script supposed to return some calculations and some plots. After connecting the submit button to the script, how do I display my results and plots in a window?

Janey,

···

On Sat, Jul 17, 2010 at 6:28 PM, C M cmpython@gmail.com wrote:

Do you know how to bind events to buttons?

Uh-huh.

Sorry, that was not meant for Steven but for Janey the OP, who I assumed was just learning. :smiley:

It was ambiguous the way I wrote it.

Che

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

I recommend Matplolib, and wxmpl to help embed it in a wxPython app.

-Chris

···

On 7/18/10 4:09 PM, Janey Duong wrote:

I actually have another question and hopefully you can help me out again here. My python script supposed to return some calculations and some plots. After connecting the submit button to the script, how do I display my results and plots in a window?

I actually have another question and hopefully you can help me out again here. My python script supposed to return some calculations and some plots. After connecting the submit button to the script, how do I display my results and plots in a window?

If you just need something quick and simple, see the PyPlot widget in the wxPython demo (if you haven’t downloaded the demo and tried it yet, that’s a must!). If you’ll want to make nicer and more complex graphs, then as Chris wrote, matplotlib is a good choice. There is a tutorial about embedding a matplotlib plot in wxPython here (without anything extra):

http://www.scipy.org/Matplotlib_figure_in_a_wx_panel

Che

Hi there,

Thank you very much for your help, but I am still having hard time using the self.TextCtrl.GetValue()

Right now, I have 2 sets of python codes, say script.py and gui.py

In my script.py, I declared some global variables and wrote some functions as follow:

VARIABLE1 = 1

VARIABLE2 = 2

VARIABLE3 = 3

···

#====================

FUNCTIONS

#====================

def func1():

def func2():

def func3():

if name ==“main”:

func1()

func2()

funce()

In my gui.py, I have

class DemoFrame(wx.Frame):

def __init__(self, parent, title):

def OnClick(self):

When I tried to do VARIABLE1 = self.TextCtrl.GetValue(), I received an error that said name ‘self’ is not defined

Please help me to fix this error!

Thanks,

Janey

On Mon, Jul 19, 2010 at 6:46 AM, C M cmpython@gmail.com wrote:

I actually have another question and hopefully you can help me out again here. My python script supposed to return some calculations and some plots. After connecting the submit button to the script, how do I display my results and plots in a window?

If you just need something quick and simple, see the PyPlot widget in the wxPython demo (if you haven’t downloaded the demo and tried it yet, that’s a must!). If you’ll want to make nicer and more complex graphs, then as Chris wrote, matplotlib is a good choice. There is a tutorial about embedding a matplotlib plot in wxPython here (without anything extra):

http://www.scipy.org/Matplotlib_figure_in_a_wx_panel

Che

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Hi Janey,

txt_ex.py (828 Bytes)

···

On Mon, Jul 19, 2010 at 2:38 PM, Janey Duong janey.duong@gmail.com wrote:

Hi there,

Thank you very much for your help, but I am still having hard time using the self.TextCtrl.GetValue()

Right now, I have 2 sets of python codes, say script.py and gui.py

In my script.py, I declared some global variables and wrote some functions as follow:

VARIABLE1 = 1

VARIABLE2 = 2

VARIABLE3 = 3

#====================

FUNCTIONS

#====================

def func1():

def func2():

def func3():

if name ==“main”:

func1()
func2()
funce()

In my gui.py, I have

class DemoFrame(wx.Frame):

def __init__(self, parent, title):
def OnClick(self):

When I tried to do VARIABLE1 = self.TextCtrl.GetValue(), I received an error that said name ‘self’ is not defined

Please help me to fix this error!

Thanks,

Janey

Have you read any of the tutorials on the wxPython website or wiki? Actually, you probably need to read through Dive Into Python or some other basic Introductory book on Python. Regardless, this code doesn’t run because you don’t instantiate anything. You aren’t starting a mainloop, creating a text control or a button or even binding the button to an event. I have attached a simple example though, which will hopefully clear up the confusion.

Mike Driscoll

Blog: http://blog.pythonlibrary.org

Dear Mike,

Sorry for the confusion, I actually have read the wxPython tutorials, and here’s is a part of my gui.py

class PlanningFrame(wx.Frame):

def **__init__**(*self*, parent, title):

    wx.Frame.__init__(*self*, parent, title=title, size=(2000,1000))

    

    # change the frame background color

    *self*.SetBackgroundColour(*"Silver"*)

    

    # create some sizers

    mainSizer = wx.BoxSizer(wx.VERTICAL)

    grid = wx.GridBagSizer(hgap=5, vgap=5)

    hSizer = wx.BoxSizer(wx.HORIZONTAL)

    

    *self*.values = wx.StaticText(*self*, label=*"DEFAULT VALUES: "*)

    grid.Add(*self*.values, pos=(0,0))



    # add the submit button

    *self*.button = wx.Button(*self*, label=*"Submit"*)

    *self*.Bind(wx.EVT_BUTTON, *self*.OnClick,*self*.button)



    # add standard deviation of the noise

    *self*.sigma = wx.StaticText(*self*, label=*"Standard deviation of the noise: "*)

    grid.Add(*self*.sigma, pos=(1,0))

    *self*.edit_sigma = wx.TextCtrl(*self*, value=*"Ex: Sigma = 50"*, size=(150,-1))

    grid.Add(*self*.edit_sigma, pos=(1,1))

    # add some other text boxes...

          ...

          ...

def OnClick(self,e):

    # Create a message dialog box

    dlg = wx.MessageDialog(*self*, *"Information successfully submitted!"*, *"Result"*, wx.OK)

    dlg.ShowModal() # Shows it

    dlg.Destroy() # finally destroy it when finished.

app = wx.App(False)

frame = PlanningFrame(None, “Iterative Planning”)

frame.Show()

app.MainLoop()

My problem is, I want to be able to put my script.py and gui.py together, so that say the VARIABLE1 in my script.py takes on self.edit_sigma.GetValue()in my gui.py. I am hoping after clicking the submit button (my button now only returns a message box), my script.py will be executed and I will be able to display whatever result script.py give me in the message box.

Thank you,

Janey

···

On Mon, Jul 19, 2010 at 12:50 PM, Mike Driscoll mike@pythonlibrary.org wrote:

Hi Janey,

On Mon, Jul 19, 2010 at 2:38 PM, Janey Duong janey.duong@gmail.com wrote:

Hi there,

Thank you very much for your help, but I am still having hard time using the self.TextCtrl.GetValue()

Right now, I have 2 sets of python codes, say script.py and gui.py

In my script.py, I declared some global variables and wrote some functions as follow:

VARIABLE1 = 1

VARIABLE2 = 2

VARIABLE3 = 3

#====================

FUNCTIONS

#====================

def func1():

def func2():

def func3():

if name ==“main”:

func1()
func2()
funce()

In my gui.py, I have

class DemoFrame(wx.Frame):

def __init__(self, parent, title):
def OnClick(self):

When I tried to do VARIABLE1 = self.TextCtrl.GetValue(), I received an error that said name ‘self’ is not defined

Please help me to fix this error!

Thanks,

Janey

Have you read any of the tutorials on the wxPython website or wiki? Actually, you probably need to read through Dive Into Python or some other basic Introductory book on Python. Regardless, this code doesn’t run because you don’t instantiate anything. You aren’t starting a mainloop, creating a text control or a button or even binding the button to an event. I have attached a simple example though, which will hopefully clear up the confusion.

Mike Driscoll

Blog: http://blog.pythonlibrary.org

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Ok. In your gui script, import script. Then in the OnClick method,
call script.calculateWhatever(*args). Something like this:

result = script.calculateWhatever(*args)
self.textCtrl.SetValue(result)

If you know the calculation will take a while, then run the script.py
file stuff in a thread and use wx.CallAfter to send the result back to
the GUI. See LongRunningTasks - wxPyWiki for more info.

···

On Jul 19, 3:03 pm, Janey Duong <janey.du...@gmail.com> wrote:

Dear Mike,

Sorry for the confusion, I actually have read the wxPython tutorials, and
here's is a part of my gui.py

class *PlanningFrame*(wx.Frame):

def \*\_\_init\_\_\*\(\*self\*, parent, title\):

    wx\.Frame\.\_\_init\_\_\(\*self\*, parent, title=title, size=\(2000,1000\)\)

    \# change the frame background color

    \*self\*\.SetBackgroundColour\(\*&quot;Silver&quot;\*\)

    \# create some sizers

    mainSizer = wx\.BoxSizer\(wx\.VERTICAL\)

    grid = wx\.GridBagSizer\(hgap=5, vgap=5\)

    hSizer = wx\.BoxSizer\(wx\.HORIZONTAL\)

    \*self\*\.values = wx\.StaticText\(\*self\*, label=\*&quot;DEFAULT VALUES: &quot;\*\)

    grid\.Add\(\*self\*\.values, pos=\(0,0\)\)

    \# add the submit button

    \*self\*\.button = wx\.Button\(\*self\*, label=\*&quot;Submit&quot;\*\)

    \*self\*\.Bind\(wx\.EVT\_BUTTON, \*self\*\.OnClick,\*self\*\.button\)

    \# add standard deviation of the noise

    \*self\*\.sigma = wx\.StaticText\(\*self\*, label=\*&quot;Standard deviation of

the noise: "*)

    grid\.Add\(\*self\*\.sigma, pos=\(1,0\)\)

    \*self\*\.edit\_sigma = wx\.TextCtrl\(\*self\*, value=\*&quot;Ex: Sigma = 50&quot;\*,

size=(150,-1))

    grid\.Add\(\*self\*\.edit\_sigma, pos=\(1,1\)\)

    \# add some other text boxes\.\.\.

          \.\.\.

          \.\.\.

def \*OnClick\*\(\*self\*,e\):

    \# Create a message dialog box

    dlg = wx\.MessageDialog\(\*self\*, \*&quot;Information successfully

submitted!"*, *"Result"*, wx.OK)

    dlg\.ShowModal\(\) \# Shows it

    dlg\.Destroy\(\) \# finally destroy it when finished\.

app = wx.App(False)

frame = PlanningFrame(None, *"Iterative Planning"*)

frame.Show()

app.MainLoop()

My problem is, I want to be able to put my script.py and gui.py together, so
that say the VARIABLE1 in my script.py takes on *self*.edit_sigma.GetValue()in
my gui.py. I am hoping after clicking the submit button (my button now only
returns a message box), my script.py will be executed and I will be able to
display whatever result script.py give me in the message box.

Thank you,

Janey

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

Blog: http://blog.pythonlibrary.org