954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

storing values with button

Hey there,
i would like to do a rank of the averages, but when i click in the button ranking, it says that global name 'media1' is not defined.

can someone help me please? thanks in

# -*- coding: cp1252 -*-
import wx
class duff(wx.Frame):
 
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,wx.ID_ANY,'Duffan Formula', size=(400,370)) #wx.ID_ANY
        self.panel = wx.Panel(self) #self.panel
 
        #self.panel on all
        g=wx.StaticText(self.panel,-1,"________________________________________________________",pos=(10,65))
        z=wx.StaticText(self.panel,-1,"Welcome to Duffan Formula!!",pos=(10,10))
        r=wx.StaticText(self.panel,-1,"Here you can discover the score of any girl you are looking for.",pos=(10,30))
        q=wx.StaticText(self.panel,-1,"Use it, with Knowledge!",pos=(10,50))
        a=wx.StaticText(self.panel,-1,"What`s her name?",pos=(10,110))
        b=wx.StaticText(self.panel,-1,"What`s her beauty score?",pos=(10,150))
        c=wx.StaticText(self.panel,-1,"What`s her body score?",pos=(10,190))
        average=wx.StaticText(self.panel,-1,"Average 1: ", pos=(15,250)).SetForegroundColour('blue')
        averagetwo=wx.StaticText(self.panel,-1,"Average 2: ", pos=(15,270)).SetForegroundColour('blue')
        averagethree=wx.StaticText(self.panel,-1,"Average 3: ", pos=(15,290)).SetForegroundColour('blue')
 
        self.one=wx.TextCtrl(self.panel,-1,'Enter name',pos=(110,108),size=(80,20))
        self.two=wx.SpinCtrl(self.panel,-1,'1',pos=(150,148),size=(60,20),min=0,max=100)
        self.three=wx.SpinCtrl(self.panel,-1,'1',pos=(130,188),size=(60,20),min=0,max=100)
        wx.StaticBox(self.panel,-1,'Personal Info', (5,90),size=(250,230))
 
 
        girlone=wx.Button(self.panel,label="Girl 1",pos=(10,220),size=(60,20))
        girlone.Bind(wx.EVT_BUTTON,self.clickbutton1)

        girltwo=wx.Button(self.panel,label="Girl 2",pos=(80,220),size=(60,20))
        girltwo.Bind(wx.EVT_BUTTON,self.clickbutton2)

        girlthree=wx.Button(self.panel,label="Girl 3",pos=(150,220),size=(60,20))
        girlthree.Bind(wx.EVT_BUTTON,self.clickbutton3)

        rank = wx.Button(self.panel, label="Ranking:", pos=(280, 80),size=(60,30))
        rank.Bind(wx.EVT_BUTTON, self.clickrank)

        self.Centre()
        self.Show(True)
 
        
             
    def clickbutton1(self,event):
        name1=self.one.GetValue()
        r1=self.two.GetValue()
        c1=self.three.GetValue()
        media1 = (r1*3 +c1*2)/5
        wx.StaticText(self.panel,-1, str(media1) ,pos=(80,250)).SetForegroundColour('blue') #str(media)

    def clickbutton2(self,event):
        name2=self.one.GetValue()
        r2=self.two.GetValue()
        c2=self.three.GetValue()
        media2 = (r2*3 +c2*2)/5
        wx.StaticText(self.panel,-1, str(media2) ,pos=(80,270)).SetForegroundColour('blue') #str(media)

    def clickbutton3(self,event):
        name3=self.one.GetValue()
        r3=self.two.GetValue()
        c3=self.three.GetValue()
        media3 = (r3*3 +c3*2)/5
        wx.StaticText(self.panel,-1, str(media3) ,pos=(80,290)).SetForegroundColour('blue') #str(media)        

    def clickrank(self,event):
        wx.StaticText(self.panel,-1, "1º:" ,pos=(260,120)).SetForegroundColour('blue') #str(media)
        wx.StaticText(self.panel,-1, "2º:" ,pos=(260,140)).SetForegroundColour('blue') #str(media)
        wx.StaticText(self.panel,-1, "3º:" ,pos=(260,160)).SetForegroundColour('blue') #str(media)
        
        if media1>media2>media3:
            wx.StaticText(self.panel,-1, "guy" ,pos=(260,160)).SetForegroundColour('blue') #str(media)
            wx.StaticText(self.panel,-1, "hyi" ,pos=(260,160)).SetForegroundColour('blue') #str(media)
            wx.StaticText(self.panel,-1, "ytg" ,pos=(260,160)).SetForegroundColour('blue') #str(media)
        
        
if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=duff(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
duffsil
Newbie Poster
6 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

You have much dublication of code, which makes your code difficult to maintain. Please refactor.

Your choice of user interface is also not to my liking, I would prefer interface with add and modify buttons and grid in ranking order for any number of girls with their own names instead of Average 1 or Girl 1.

Use sequence types like list or tuple or objects instead of individual variables. I do not know wx enough to suggest appropriate grid to you.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

You must store the averages somewhere in the instance

# -*- coding: cp1252 -*-
import wx
from functools import partial

class duff(wx.Frame):
 
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,wx.ID_ANY,'Duffan Formula', size=(400,370)) #wx.ID_ANY
        self.panel = wx.Panel(self) #self.panel
 
        #self.panel on all
        g=wx.StaticText(self.panel,-1,"________________________________________________________",pos=(10,65))
        z=wx.StaticText(self.panel,-1,"Welcome to Duffan Formula!!",pos=(10,10))
        r=wx.StaticText(self.panel,-1,"Here you can discover the score of any girl you are looking for.",pos=(10,30))
        q=wx.StaticText(self.panel,-1,"Use it, with Knowledge!",pos=(10,50))
        a=wx.StaticText(self.panel,-1,"What`s her name?",pos=(10,110))
        b=wx.StaticText(self.panel,-1,"What`s her beauty score?",pos=(10,150))
        c=wx.StaticText(self.panel,-1,"What`s her body score?",pos=(10,190))
        average=wx.StaticText(self.panel,-1,"Average 1: ", pos=(15,250)).SetForegroundColour('blue')
        averagetwo=wx.StaticText(self.panel,-1,"Average 2: ", pos=(15,270)).SetForegroundColour('blue')
        averagethree=wx.StaticText(self.panel,-1,"Average 3: ", pos=(15,290)).SetForegroundColour('blue')
 
        self.one=wx.TextCtrl(self.panel,-1,'Enter name',pos=(110,108),size=(80,20))
        self.two=wx.SpinCtrl(self.panel,-1,'1',pos=(150,148),size=(60,20),min=0,max=100)
        self.three=wx.SpinCtrl(self.panel,-1,'1',pos=(130,188),size=(60,20),min=0,max=100)
        wx.StaticBox(self.panel,-1,'Personal Info', (5,90),size=(250,230))
 
 
        for i in range(3):
            girl=wx.Button(self.panel,label="Girl %d" % (i+1),pos=(10+70*i,220),size=(60,20))
            girl.Bind(wx.EVT_BUTTON,partial(self.clickbutton, index=i))

        rank = wx.Button(self.panel, label="Ranking:", pos=(280, 80),size=(60,30))
        rank.Bind(wx.EVT_BUTTON, self.clickrank)

        self.media = [0,0,0] # store the girls scores
        self.Centre()
        self.Show(True)
 
        
             
    def clickbutton(self,event, index=0):
        name, r, c = [x.GetValue() for x in self.one, self.two, self.three]
        media = self.media[index] = (r*3 +c*2)/5
        wx.StaticText(self.panel,-1, str(media),
            pos=(90,250+20*index)).SetForegroundColour('blue') #str(media)


    def clickrank(self,event):
        wx.StaticText(self.panel,-1, "1º:" ,pos=(260,120)).SetForegroundColour('blue') #str(media)
        wx.StaticText(self.panel,-1, "2º:" ,pos=(260,140)).SetForegroundColour('blue') #str(media)
        wx.StaticText(self.panel,-1, "3º:" ,pos=(260,160)).SetForegroundColour('blue') #str(media)
        m1, m2, m3 = self.media
        if m1 > m2 > m3:
            wx.StaticText(self.panel,-1, "guy" ,pos=(280,120)).SetForegroundColour('blue') #str(media)
            wx.StaticText(self.panel,-1, "hyi" ,pos=(280,140)).SetForegroundColour('blue') #str(media)
            wx.StaticText(self.panel,-1, "ytg" ,pos=(280,160)).SetForegroundColour('blue') #str(media)
        
        
if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=duff(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

thank you but now i wanna put the names into the rank. how do i do that?

duffsil
Newbie Poster
6 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 
>>> from collections import namedtuple
>>> Girl = namedtuple(Girl, 'name beauty body')
>>> girls = (Girl('Jill', 20, 40), Girl('Joy', 10, 50), Girl('Jane', 50, 10))
>>> print girls
(Girl(name='Jill', beauty=20, body=40), Girl(name='Joy', beauty=10, body=50), Girl(name='Jane', beauty=50, body=10))
>>> def score(g):
	return (g.beauty * 3 + g.body * 2) / 5
>>> for girl in girls: print girl.name, score(girl)

Jill 28
Joy 26
Jane 34
>>> print sorted(girls, key=score, reverse=True)
[Girl(name='Jane', beauty=50, body=10), Girl(name='Jill', beauty=20, body=40), Girl(name='Joy', beauty=10, body=50)]
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 


^^ ????!?!?!?

duffsil
Newbie Poster
6 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

Sorry the definition of Girl as namedtuple should have been:

>>> Girl = namedtuple('Girl', 'name beauty body')

at second line. And the code is interactive prompt log on sorting the girls by ranking function score.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

still dont understand. can you please send all of the codes together so that i can understand it better. thanks in advance

duffsil
Newbie Poster
6 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: