Help...get 2 values in a same wx.Textctrl

Good morning brothers,

I am new to Wxpython and’m breaking my head on a matter.

I created a calculator and one of calculations, I am trying to create a button that prompts the user to enter the radicand and the index, but in a single wx.Textctrl.

Follows the source code. I reduced to be more objective. :slight_smile:

-- coding: cp1252 --

import wx

import math

class MyFrame(wx.Frame):

def init(self, parent, id):

wx.Frame.init(self, parent, id, title = ‘Test’, size = (400, 300), style = wx.DEFAULT_FRAME_STYLE)

self.panel = wx.Panel(self)

self.panel.SetBackgroundColour(’#E6E6FA’)

self.display = wx.TextCtrl(self.panel, -1, ‘’, (10,10),(360, 60), style = wx.TE_RIGHT)

self.display.SetFont(wx.Font(28, wx.DEFAULT, wx.NORMAL, wx.BOLD))

self.display.SetMaxLength(13)

self.calc = wx.Button(self.panel, 10,‘Root’, (10, 210),(70, 30))

self.calc2 = wx.Button(self.panel, 11,‘Root2’, (80,210),(70, 30))

self.Bind(wx.EVT_BUTTON, self.OnRoot,id=10)

self.Bind(wx.EVT_BUTTON, self.OnRoot2,id=11)

def OnRoot(self, event): #This it’s ok

a = self.display.GetValue()

b = math.sqrt(float(a))

self.display.Clear()

self.display.AppendText(str(b))

def OnRoot2(self, event): #But in here i am not knowing to resolve

a = self.display.GetValue()

self.display.Clear()

“”"

def Root(self, a, b): This it’s a calculation possibly, but i don’t know how put two arguments in a Textctrl.

return a**1.0/b

“”"

if name==‘main’:

frame = wx.App()

frm = MyFrame(parent= None, id = -1)

frm.Center()

frm.Show()

frame.MainLoop()

Please, help me…

A great day for all!!!

Jonathan Bastos Ferraz wrote:

I am new to Wxpython and'm breaking my head on a matter.
I created a calculator and one of calculations, I am trying to create
a button that prompts the user to enter the radicand and the index,
but in a single wx.Textctrl.

This is the same problem faced by every hand-held calculator maker. You
really have two choices. You will either have to put in two text boxes,
or you will have to store the first value while you ask for the second.
That means you'll need some kind of prompt to tell the user what you're
waiting for.

HP calculators do this with RPN: you have the user enter a number, then
click "Push" or "Enter" or something to save it away, then enter the
second number, then click the operation to be done. The other approach
would be to enter the first number, click "Root 2", then put up a
message saying "You entered X.XXXX, now enter the second value". That
means your code would need to remember it was in the middle of an operation.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Thanks for answer Tim Roberts

I understand, but that’s what I thought, put a value then the other, only then make a calculation, the problem I am unable to put in the code. Do you have any ideas?

···

Em segunda-feira, 9 de março de 2015 14:19:40 UTC-3, Tim Roberts escreveu:

Jonathan Bastos Ferraz wrote:

I am new to Wxpython and’m breaking my head on a matter.

I created a calculator and one of calculations, I am trying to create

a button that prompts the user to enter the radicand and the index,

but in a single wx.Textctrl.

This is the same problem faced by every hand-held calculator maker. You

really have two choices. You will either have to put in two text boxes,

or you will have to store the first value while you ask for the second.
That means you’ll need some kind of prompt to tell the user what you’re

waiting for.

HP calculators do this with RPN: you have the user enter a number, then

click “Push” or “Enter” or something to save it away, then enter the

second number, then click the operation to be done. The other approach

would be to enter the first number, click “Root 2”, then put up a

message saying “You entered X.XXXX, now enter the second value”. That

means your code would need to remember it was in the middle of an operation.


Tim Roberts, ti...@probo.com

Providenza & Boekelheide, Inc.

Jonathan Bastos Ferraz wrote:

Thanks for answer Tim Roberts

I understand, but that's what I thought, put a value then the other,
only then make a calculation, the problem I am unable to put in the
code. Do you have any ideas?

Your code has to remember this. You need to have "state" in your code
that knows where you are in the process.

Consider code like this, for example. Assume that you have added a
StaticText control called "status" to give the user feedback:

    def OnRoot2(self,event):
        if self.first is None:
            self.first = self.display.GetValue()
            self.status.SetLabel( "You entered %s for a. Now enter b."
% self.first )
            self.display.Clear()
        else:
            a = self.display.GetValue()
            # Do computation with self.first and a and put result in
self.display.
            self.first = None
            self.status.SetLabel( "" )

You'll need to define self.first = None in your __init__ so it has a
value. You'll also need to consider what happens if the user enters a
number and presses "root2", and then decides to be obnoxious and press
"root". You're left in the middle of a computation. Do you just
abandon the first one? If so (and that's a reasonable path), you'll
need to clear out "self.first". Another reasonable path would be to
disable the "root" button when you're in the middle of "root2".

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.