I began learning the Python language only 2 days ago, so there is surely much for me to learn. Still I’ve done a fair amount of studying and gotten up to writing code using wxPython.
I’m currently trying to write a program to simulate a simple Calculator. It’s just an experiment to see if I can do it. I’ve hit a snag though as my program relies upon a count being initiated once the user clicks on a button. I’m currently working on a much smaller code; if I can make this code work I’ll be able to figure out how to make my real code work.
Here is the sample code (keep in mind that I’d typically have everything indented properly; can’t do it here for some reason)
import wx
class Action(wx.Frame):
def init(self,parent,id):
wx.Frame.init(self,parent,id,‘Frame’,size=(750,600))
global panel
panel=wx.Panel(self)
**
self.Button2=wx.Button(panel,label=“Attempt”,pos=(130,200),size=(60,60))
self.Bind(wx.EVT_BUTTON,self.Attempt,self.Button2)
count = 0
def Attempt(self,event):
count = count + 1
if (count == 1):
print("Program works")
**
if name==‘main’:
app=wx.PySimpleApp() #This runs program
frame=Action(parent=None,id=-1) # Displays the program
frame.Show() # Show the frame
app.MainLoop() # Run the application
The important part of the code is put in bold. The point here is trying to print the message “Program works” as to indicate that the count has incremented due to the event associated with clicking the Attempt button. I get an error message saying :
" count = count + 1
UnboundLocalError: local variable ‘count’ referenced before assignment"
Again if I can somehow associate a number being incremented due to the use of a button than my real program should work. I’d need to access that number as global variables. So, does anyone have any suggestions?