Member Avatar for sravan953

Hey guys,

I was just making a tiny program, where I need the user to enter numbers. My question is, how do get numbers?

I tried:

self.panel=wx.Panel(None,-1)
self.n=wx.TextCtrl(self.panel,-1,"Number")
self.get=int(self.n.GetValue())

-but that didn't work... so how do I do it?

Your going to have to GetValue() in a function of some sort, after user input. I suggest binding to some event, either EVT_TEXT, EVT_TEXT_ENTER and GetValue() there, or place some sort of button that the user can push when they're done with input.

Like winmic says you need to use GetValue() in a method.
You have also "event.GetKeyCode()".
That can record live from keyboard.
But most normal is to have av button that collect data from user_input and do stuff.

Here a function(or method as is called when in a class)
This function collect user_input from 2 wx.TextCtrl.
And calculate the numbers from use_input and output to a wx.StaticText(SetLabel)

def button_click(self, event):         
    self.s1 = self.tex1.GetValue()
    self.s2 = self.tex2.GetValue()
    calculate = self.s1 * self.s2  
    self.l2.SetLabel (str(calculate))
Member Avatar for sravan953

Here is the code I have till now:

import wx

window=wx.App()

class s_report(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Grade It!",size=(500,700))

        self.panel=wx.Panel(self,-1)

        menu=wx.MenuBar()

        file_menu=wx.Menu()
        file_menu.Append(301,"Quit")
        self.Bind(wx.EVT_MENU,self.Quit,id=301)

        help_menu=wx.Menu()
        help_menu.Append(302,"About")
        self.Bind(wx.EVT_MENU,self.about,id=302)

        menu.Append(file_menu,"File")
        menu.Append(help_menu,"Help")

        self.SetMenuBar(menu)

        wx.StaticText(self.panel,-1,"Welcome to Grade It!.\nPlease enter your exam marks for 12 subjects. Your highest\nscoring and lowest scoring subject, along with your percentage and grade will be displayed.",pos=(10,10))
        wx.StaticText(self.panel,-1,"Please enter the marks of 12 subjects:",pos=(10,70))

        wx.StaticText(self.panel,-1,"English Language:",pos=(10,100))
        wx.StaticText(self.panel,-1,"English Literature:",pos=(10,125))
        wx.StaticText(self.panel,-1,"Mathematics:",pos=(10,150))
        wx.StaticText(self.panel,-1,"Language II",pos=(10,175))
        wx.StaticText(self.panel,-1,"Physics:",pos=(10,200))
        wx.StaticText(self.panel,-1,"Chemistry:",pos=(10,225))
        wx.StaticText(self.panel,-1,"Biology:",pos=(10,250))
        wx.StaticText(self.panel,-1,"History/Civics:",pos=(10,275))
        wx.StaticText(self.panel,-1,"Geography:",pos=(10,300))
        wx.StaticText(self.panel,-1,"Environmental Education",pos=(10,325))
        wx.StaticText(self.panel,-1,"Computer Science",pos=(10,350))
        wx.StaticText(self.panel,-1,"Computer Practicals",pos=(10,375))

        self.one=wx.TextCtrl(self.panel,-1,pos=(150,100),size=(40,20))
        self.two=wx.TextCtrl(self.panel,-1,pos=(150,125),size=(40,20))
        self.three=wx.TextCtrl(self.panel,-1,pos=(150,150),size=(40,20))
        self.four=wx.TextCtrl(self.panel,-1,pos=(150,175),size=(40,20))
        self.five=wx.TextCtrl(self.panel,-1,pos=(150,200),size=(40,20))
        self.six=wx.TextCtrl(self.panel,-1,pos=(150,225),size=(40,20))
        self.seven=wx.TextCtrl(self.panel,-1,pos=(150,250),size=(40,20))
        self.eight=wx.TextCtrl(self.panel,-1,pos=(150,275),size=(40,20))
        self.nine=wx.TextCtrl(self.panel,-1,pos=(150,300),size=(40,20))
        self.ten=wx.TextCtrl(self.panel,-1,pos=(150,325),size=(40,20))
        self.eleven=wx.TextCtrl(self.panel,-1,pos=(150,350),size=(40,20))
        self.twelve=wx.TextCtrl(self.panel,-1,pos=(150,375),size=(40,20))

        wx.Button(self.panel,201,"Done",pos=(130,400))
        self.Bind(wx.EVT_BUTTON,self.Done,id=201)

        self.Centre()
        self.Show()

    def Done(self,event):

        self.marks=[]
        
        self.marks.append(self.one.GetValue())
        self.marks.append(self.two.GetValue())
        self.marks.append(self.three.GetValue())
        self.marks.append(self.four.GetValue())
        self.marks.append(self.five.GetValue())
        self.marks.append(self.six.GetValue())
        self.marks.append(self.seven.GetValue())
        self.marks.append(self.eight.GetValue())
        self.marks.append(self.nine.GetValue())
        self.marks.append(self.ten.GetValue())
        self.marks.append(self.eleven.GetValue())
        self.marks.append(self.twelve.GetValue())

        self.average=sum(self.marks)/len(self.marks)

        wx.StaticText(self.panel,-1,self.average,pos=(10,425))
        
    def Quit(self,event):
        self.Close()

    def about(self,event):
        self.about=wx.AboutDialogInfo()

        self.about.SetName("Grade It!")
        self.about.SetCopyright("(c) 2009 Sravan")
        self.about.SetWebSite("http://www.uberpix.wordpress.com")
        self.about.AddDeveloper("Sravan")

        wx.AboutBox(self.about)

s_report()
window.MainLoop()

If enter number and push 'Done', it gives this error:

Traceback (most recent call last):
File "C:\Documents and Settings\sravan953\My Documents\Sravan\PYTHON\Grade It!.py", line 79, in Done
self.average=sum(self.marks)/len(self.marks)
TypeError: unsupported operand type(s) for +: 'int' and 'unicode'

self.marks.append(self.twelve.GetValue())
        self.average=sum(self.marks)/len(self.marks)

Note that GetValue returns a string. In order to use sum you'll need to convert all elements of self.marks to integers or floating points. You could probably get away with using a try / except block like this:

>>> marks = ['1','2.5','3','4.2','5','6.6666','7.111']
>>> new_marks = []
>>> for mark in marks:
...     try:
...         new_marks.append(int(mark))
...     except ValueError:
...         new_marks.append(float(mark))
...     
>>> new_marks
[1, 2.5, 3, 4.2000000000000002, 5, 6.6665999999999999, 7.1109999999999998]
>>> sum(new_marks)/len(new_marks)
4.2110857142857139

As you can see I try to convert to int first, and if that fails (as a string representing a floating point will raise a ValueError in the int method), I'll then convert to float . This protects me from failure and allows the script to convert correctly between string to int and/or float.

At this point, you should be okay using the sum function, as your list elements will all be numerical instead of strings.

Alternately, you could use list comprehension to convert to floats on the fly like this:

>>> marks = ['1','2.5','3','4.2','5','6.6666','7.111']
>>> sum([float(mark) for mark in marks])/len(marks)
4.2110857142857139

In this version I only use float, as both an int and float will convert to float whereas a float will not convert to an int so this avoids the failure...
To break that down step by step the list comprehension becomes:

>>> new_marks = []
>>> for mark in marks:
...     new_marks.append(float(mark))
...     
>>> sum(new_marks)/len(new_marks)
4.2110857142857139
>>>

If you have questions about list comprehension just ask and I can try to explain; otherwise here's the documentation for them: here.

def Done(self,event):
        
        self.marks=[] 
        
        self.marks.append(self.one.GetValue())
        self.marks.append(self.two.GetValue())
        self.marks.append(self.three.GetValue())
        self.marks.append(self.four.GetValue())
        self.marks.append(self.five.GetValue())
        self.marks.append(self.six.GetValue())
        self.marks.append(self.seven.GetValue())
        self.marks.append(self.eight.GetValue())
        self.marks.append(self.nine.GetValue())
        self.marks.append(self.ten.GetValue())
        self.marks.append(self.eleven.GetValue())
        self.marks.append(self.twelve.GetValue())
        
        print self.marks  #test print

I had a look at this,and something that wonder about.
Is why many GetValue() that is put in a list return a unicode list?

Here is the test print,just typed in four number.

[u'5', u'', u'', u'9', u'', u'7', u'', u'', u'', u'3', u'', u'']

Hey I took your code and played with it try this

import wx

window=wx.App()

class s_report(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Grade It!",size=(500,700))

        self.panel=wx.Panel(self,-1)

        menu=wx.MenuBar()

        file_menu=wx.Menu()
        file_menu.Append(301,"Quit")
        self.Bind(wx.EVT_MENU,self.Quit,id=301)

        help_menu=wx.Menu()
        help_menu.Append(302,"About")
        self.Bind(wx.EVT_MENU,self.about,id=302)

        menu.Append(file_menu,"File")
        menu.Append(help_menu,"Help")

        self.SetMenuBar(menu)

        wx.StaticText(self.panel,-1,"Welcome to Grade It!.\nPlease enter your exam marks for 12 subjects. Your highest\nscoring and lowest scoring subject, along with your percentage and grade will be displayed.",pos=(10,10))
        wx.StaticText(self.panel,-1,"Please enter the marks of 12 subjects:",pos=(10,70))

        wx.StaticText(self.panel,-1,"English Language:",pos=(10,100))
        wx.StaticText(self.panel,-1,"English Literature:",pos=(10,125))
        wx.StaticText(self.panel,-1,"Mathematics:",pos=(10,150))
        wx.StaticText(self.panel,-1,"Language II",pos=(10,175))
        wx.StaticText(self.panel,-1,"Physics:",pos=(10,200))
        wx.StaticText(self.panel,-1,"Chemistry:",pos=(10,225))
        wx.StaticText(self.panel,-1,"Biology:",pos=(10,250))
        wx.StaticText(self.panel,-1,"History/Civics:",pos=(10,275))
        wx.StaticText(self.panel,-1,"Geography:",pos=(10,300))
        wx.StaticText(self.panel,-1,"Environmental Education",pos=(10,325))
        wx.StaticText(self.panel,-1,"Computer Science",pos=(10,350))
        wx.StaticText(self.panel,-1,"Computer Practicals",pos=(10,375))

        self.one=wx.TextCtrl(self.panel,-1,pos=(150,100),size=(40,20))
        self.two=wx.TextCtrl(self.panel,-1,pos=(150,125),size=(40,20))
        self.three=wx.TextCtrl(self.panel,-1,pos=(150,150),size=(40,20))
        self.four=wx.TextCtrl(self.panel,-1,pos=(150,175),size=(40,20))
        self.five=wx.TextCtrl(self.panel,-1,pos=(150,200),size=(40,20))
        self.six=wx.TextCtrl(self.panel,-1,pos=(150,225),size=(40,20))
        self.seven=wx.TextCtrl(self.panel,-1,pos=(150,250),size=(40,20))
        self.eight=wx.TextCtrl(self.panel,-1,pos=(150,275),size=(40,20))
        self.nine=wx.TextCtrl(self.panel,-1,pos=(150,300),size=(40,20))
        self.ten=wx.TextCtrl(self.panel,-1,pos=(150,325),size=(40,20))
        self.eleven=wx.TextCtrl(self.panel,-1,pos=(150,350),size=(40,20))
        self.twelve=wx.TextCtrl(self.panel,-1,pos=(150,375),size=(40,20))

        wx.Button(self.panel,201,"Done",pos=(130,400))
        self.Bind(wx.EVT_BUTTON,self.Done,id=201)

        self.Centre()
        self.Show()

    def Done(self,event):

        marks=[]
        
        marks.append(int(self.one.GetValue()))
        marks.append(int(self.two.GetValue()))
        marks.append(int(self.three.GetValue()))
        marks.append(int(self.four.GetValue()))
        marks.append(int(self.five.GetValue()))
        marks.append(int(self.six.GetValue()))
        marks.append(int(self.seven.GetValue()))
        marks.append(int(self.eight.GetValue()))
        marks.append(int(self.nine.GetValue()))
        marks.append(int(self.ten.GetValue()))
        marks.append(int(self.eleven.GetValue()))
        marks.append(int(self.twelve.GetValue()))
        sum = 0
        for num in marks:
          sum = sum+num

        
        
        wx.StaticText(self.panel,-1,"Average: ",pos=(10,425))
        wx.StaticText(self.panel,-1,str(sum/len(marks)),pos=(200,425))
        
    def Quit(self,event):
        self.Close()

    def about(self,event):
        self.about=wx.AboutDialogInfo()

        self.about.SetName("Grade It!")
        self.about.SetCopyright("(c) 2009 Sravan")
        self.about.SetWebSite("http://www.uberpix.wordpress.com")
        self.about.AddDeveloper("Sravan")

        wx.AboutBox(self.about)

s_report()
window.MainLoop()

Have fun

You can try this changes.

import wx

window=wx.App()

class s_report(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Grade It!",size=(500,700))

        self.panel=wx.Panel(self,-1)

        menu=wx.MenuBar()

        file_menu=wx.Menu()
        file_menu.Append(301,"Quit")
        self.Bind(wx.EVT_MENU,self.Quit,id=301)

        help_menu=wx.Menu()
        help_menu.Append(302,"About")
        self.Bind(wx.EVT_MENU,self.about,id=302)

        menu.Append(file_menu,"File")
        menu.Append(help_menu,"Help")

        self.SetMenuBar(menu)

        wx.StaticText(self.panel,-1,"Welcome to Grade It!.\nPlease enter your exam marks for 12 subjects. Your highest\nscoring and lowest scoring subject, along with your percentage and grade will be displayed.",pos=(10,10))
        wx.StaticText(self.panel,-1,"Please enter the marks of 12 subjects:",pos=(10,70))

        wx.StaticText(self.panel,-1,"English Language:",pos=(10,100))
        wx.StaticText(self.panel,-1,"English Literature:",pos=(10,125))
        wx.StaticText(self.panel,-1,"Mathematics:",pos=(10,150))
        wx.StaticText(self.panel,-1,"Language II",pos=(10,175))
        wx.StaticText(self.panel,-1,"Physics:",pos=(10,200))
        wx.StaticText(self.panel,-1,"Chemistry:",pos=(10,225))
        wx.StaticText(self.panel,-1,"Biology:",pos=(10,250))
        wx.StaticText(self.panel,-1,"History/Civics:",pos=(10,275))
        wx.StaticText(self.panel,-1,"Geography:",pos=(10,300))
        wx.StaticText(self.panel,-1,"Environmental Education",pos=(10,325))
        wx.StaticText(self.panel,-1,"Computer Science",pos=(10,350))
        wx.StaticText(self.panel,-1,"Computer Practicals",pos=(10,375))

        self.one=wx.TextCtrl(self.panel,-1,'0',pos=(150,100),size=(40,20))
        self.two=wx.TextCtrl(self.panel,-1,'0',pos=(150,125),size=(40,20))
        self.three=wx.TextCtrl(self.panel,-1,'0',pos=(150,150),size=(40,20))
        self.four=wx.TextCtrl(self.panel,-1,'0',pos=(150,175),size=(40,20))
        self.five=wx.TextCtrl(self.panel,-1,'0',pos=(150,200),size=(40,20))
        self.six=wx.TextCtrl(self.panel,-1,'0',pos=(150,225),size=(40,20))
        self.seven=wx.TextCtrl(self.panel,-1,'0',pos=(150,250),size=(40,20))
        self.eight=wx.TextCtrl(self.panel,-1,'0',pos=(150,275),size=(40,20))
        self.nine=wx.TextCtrl(self.panel,-1,'0',pos=(150,300),size=(40,20))
        self.ten=wx.TextCtrl(self.panel,-1,'0',pos=(150,325),size=(40,20))
        self.eleven=wx.TextCtrl(self.panel,-1,'0',pos=(150,350),size=(40,20))
        self.twelve=wx.TextCtrl(self.panel,-1,'0',pos=(150,375),size=(40,20))

        wx.Button(self.panel,201,"Done",pos=(130,400))
        self.Bind(wx.EVT_BUTTON,self.Done,id=201)

        self.Centre()
        self.Show()

    def Done(self,event):        
        
        self.marks = []        
        try:           
            self.marks.append(int(self.one.GetValue()))
            self.marks.append(int(self.two.GetValue()))
            self.marks.append(int(self.three.GetValue()))
            self.marks.append(int(self.four.GetValue()))
            self.marks.append(int(self.five.GetValue()))
            self.marks.append(int(self.six.GetValue()))
            self.marks.append(int(self.seven.GetValue()))
            self.marks.append(int(self.eight.GetValue()))
            self.marks.append(int(self.nine.GetValue()))
            self.marks.append(int(self.ten.GetValue()))
            self.marks.append(int(self.eleven.GetValue()))
            self.marks.append(int(self.twelve.GetValue()))
        except:
            self.marks.append(float(self.one.GetValue()))
            self.marks.append(float(self.two.GetValue()))
            self.marks.append(float(self.three.GetValue()))
            self.marks.append(float(self.four.GetValue()))
            self.marks.append(float(self.five.GetValue()))
            self.marks.append(float(self.six.GetValue()))
            self.marks.append(float(self.seven.GetValue()))
            self.marks.append(float(self.eight.GetValue()))
            self.marks.append(float(self.nine.GetValue()))
            self.marks.append(float(self.ten.GetValue()))
            self.marks.append(float(self.eleven.GetValue()))
            self.marks.append(float(self.twelve.GetValue()))    
        
        self.average = int(sum(self.marks)) / float(12)
        my_avg = '%.2f' % (self.average)       
        wx.StaticText(self.panel,-1,str(my_avg),pos=(155,435) ).SetForegroundColour('blue') 
        
        
    def Quit(self,event):
        self.Close()

    def about(self,event):
        self.about=wx.AboutDialogInfo()

        self.about.SetName("Grade It!")
        self.about.SetCopyright("(c) 2009 Sravan")
        self.about.SetWebSite("http://www.uberpix.wordpress.com")
        self.about.AddDeveloper("Sravan")

        wx.AboutBox(self.about)

s_report()
window.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.