[Tutor] Problems with GUI calling a class

David Holland davholla2002 at yahoo.co.uk
Sat Oct 16 17:59:04 CEST 2004


Kent,

Thanks that it is the problem.
 It still has a few problems but none that I can not
solve now.

 --- Kent Johnson <kent_johnson at skillsoft.com> wrote: 
> David,
> 
> It looks like neither branch of your conditional is
> being executed. You 
> have, in outline,
>          if self.yes_no == 'N':
>              results = ...
>          elif self.yes_no == 'Y':
>              results = ...
>          self.results_txt.insert(0.0,results)
> 
> If self.yes_no is neither 'N' or 'Y' then results
> will not be initialized 
> and you will get the error you are seeing.
> 
> Looking a little above this section in your code,
> you have
>    if self.yes_no.get():
> so I am guessing that self.yes_no is a reference to
> something in the gui? 
> and not a string. Maybe the conditional that is
> failing also should be 
> using self.yes_no.get()?
> 
> Kent
> 
> At 04:32 PM 10/16/2004 +0100, David Holland wrote:
> >  I wrote a program to calculate the likely result
> of a
> >one day cricket match and then decided to make it
> into
> >a class and have another class for a GUI.
> >It all works well but I can not call the function
> from
> >the first class from the GUI class, insteading of
> >returning a result it just gives this error
> 'Exception
> >in Tkinter callback
> >Traceback (most recent call last):
> >   File "/usr/lib/python2.3/lib-tk/Tkinter.py",
> line
> >1345, in __call__
> >     return self.func(*args)
> >   File
> >"/home/david/Documents/pyprogramming/cricketv6.py",
> >line 194, in cricket_getinfo
> >     self.results_txt.insert(0.0,results)
> >UnboundLocalError: local variable 'results'
> referenced
> >before assignment
> >'
> >This is the code which create the GUI and if it
> worked
> >would call the calculations.
> >     def cricket_getinfo(self):
> >         """Get values from the GUI and submit for
> >calculation"""
> >         print "test"
> >         runs = self.runs_ent.get()
> >         wickets = self.wickets_ent.get()
> >         overs = self.overs_ent.get()
> >         if self.yes_no.get():
> >             used_before = 'Y'
> >         else:
> >             used_before = 'N'
> >         Cricketobj = Cricket()
> >         #create code to call the calculations
> >         z = 50
> >         win = 10
> >         if self.yes_no == 'N':
> >             win = 10
> >             originalscore = 0
> >             wold = 0
> >             yold = 0
> >             results =
> >Cricketobj.runfuns(wickets,overs,runs,z, win,
> >originalscore, orignovoers, wold, yold)
> >             print results
> >         elif self.yes_no == 'Y':
> >         #get the data from last time
> >             xold, yold, wold = openlasttime()
> >         #now we are calculating using the more up
> to
> >date run rate
> >             orignovoers = y
> >             runs = int(runs)
> >             xold = int(xold)
> >             x = x - xold
> >             overs = int(overs) - int(yold)
> >             wickets = int(wickets) - int(wold)
> >             wold = int(wold)
> >             win = 10
> >             z = 50
> >             results =
> >Cricketobj.runfuns(wickets,overs,runs,z, win,
> >originalscore, orignovoers, wold,
> yold)#(w,y,x,z,win,
> >xold, orignovoers, wold, yold)
> >         #the line below is causing it crash
> >         #results = 1
> >         self.results_txt.insert(0.0,results)
> >
> >Cricketobj.savecurrentstate(runs,overs,wickets)
> >
> >
> >Can anyone see why I am not calling the class's
> >function properly ?
> >
> >Thanks in advance.
> >David
> >
> >
> >
> >
> >
>
>___________________________________________________________ALL-NEW
> Yahoo! 
> >Messenger - all new features - even more fun! 
> http://uk.messenger.yahoo.com
> >_______________________________________________
> >Tutor maillist  -  Tutor at python.org
> >http://mail.python.org/mailman/listinfo/tutor
> 
>  


	
	
		
___________________________________________________________ALL-NEW Yahoo! Messenger - all new features - even more fun!  http://uk.messenger.yahoo.com


More information about the Tutor mailing list