473,385 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

simple oop question (I think) [solved]

I am just trying to acess a function in wordgrid (savefile) to a button
that is defined in TestFrame. I can't seem to make it work I either
get an error that my variable isn't global or it makes other
complaints. thanks in advance.. sorry for the simple question..
Expand|Select|Wrap|Line Numbers
  1. import wx 
  2. import wx.grid as gridlib 
  3. import sys 
  4.  
  5.  
  6. #--------------------------------------------------------------------------*- 
  7.  
  8.  
  9. class WordGrid(gridlib.Grid): 
  10.  
  11.  
  12.     def __init__(self, parent, log): 
  13.         gridlib.Grid.__init__(self, parent, -1) 
  14.         self.loadFile() 
  15.  
  16.  
  17.         self.CreateGrid(len(self.rows), self.widestRow) 
  18.  
  19.  
  20.         for r, row in enumerate(self.rows): 
  21.             for c, col in enumerate(row): 
  22.                 self.SetCellValue(r, c, col) 
  23.             self.SetColSize(c, 10*self.widestCol) 
  24.  
  25.  
  26.         for c, label in enumerate(self.header): 
  27.             self.SetColLabelValue(c, label) 
  28.  
  29.  
  30.     def loadFile(self): 
  31.        #from_file 
  32.        infile = open(sys.argv[1], 'r') #The first argument passed in is 
  33. the file name 
  34.        foundHeader = False 
  35.        self.rows = [] 
  36.        for line in infile: 
  37.            if sys.argv[2] in line: #look for the second argument and 
  38. make that the header 
  39.                #removefirst = line.split(' ') 
  40.                self.header = line.split() 
  41.                #foundHeader = 'true' 
  42.                continue     # we don't want to process this line any 
  43. further 
  44.            else: 
  45.                self.rows.append(line.split()) 
  46.  
  47.  
  48.        self.widestRow = max([len(r) for r in self.rows]) 
  49.        self.widestCol = max([len(c) for c in [r for r in self.rows]]) 
  50.     def savefile(self): 
  51.         outfile = open(sys.argv[1], 'w') #open the file defined in the 
  52. output line for writing 
  53.         for row in self.rows: 
  54.             outfile.write(row) 
  55.  
  56.  
  57.         print('this is a test to see if I can Crash it') 
  58.  
  59.  
  60. class TestFrame(wx.Frame): 
  61.     def __init__(self, parent, log): 
  62.  
  63.  
  64.         wx.Frame.__init__(self, parent, -1, "Dex Tracker Sco Editor", 
  65. size=(640,480)) 
  66.         p = wx.Panel(self, -1, style=0) 
  67.         grid = WordGrid(p, log) 
  68.         #grid = CustTableGrid(p, log) 
  69.         b = wx.Button(p, -1, "Save Grid") 
  70.         b.SetDefault() 
  71.         self.Bind(wx.EVT_BUTTON, self.OnButton, b) 
  72.         b.Bind(wx.EVT_SET_FOCUS, self.OnButtonFocus) 
  73.         bs = wx.BoxSizer(wx.VERTICAL) 
  74.         bs.Add(grid, 1, wx.GROW|wx.ALL, 5) 
  75.         bs.Add(b) 
  76.         p.SetSizer(bs) 
  77.  
  78.  
  79.     def OnButton(self, evt): 
  80.         print "button selected" 
  81.         grid = WordGrid(self, log).savefile() 
  82.         #self.WordGrid.savefile(self) 
  83.  
  84.  
  85.     def OnButtonFocus(self, evt): 
  86.         print "button focus" 
  87.  
  88.  
  89. #--------------------------------------------------------------------------*- 
  90. #def main(): 
  91.  
  92.  
  93. def main(From_File, find_string): 
  94.     """This is the entire editor for .sco files..  It doesn't realy 
  95. care if it is music or not.  Any file that you lay out with even rows 
  96. and collums 
  97.     can be displayed  The first argument passed to main is the file to 
  98. be used and the second if the string to be used as the command to set 
  99. up the header of the grid. 
  100.     The sting you wish to use to identify the header should be placed 
  101. last so it doesn't show up in the grid. 
  102.     """ 
  103.  
  104.  
  105.     import sys 
  106.  
  107.  
  108.     app = wx.PySimpleApp() 
  109.     frame = TestFrame(None, sys.stdout) 
  110.     frame.Show(True) 
  111.     app.MainLoop() 
  112.     pass 
  113.  
  114.  
  115. if __name__ == '__main__': 
  116.     import sys 
  117.     #try: 
  118.     main(sys.argv[1], sys.argv[2])
  119.  
https://sourceforge.net/projects/dex-tracker
Oct 30 '06 #1
5 1446
bartonc
6,596 Expert 4TB
Hey Eric,
It's great to see that you are using wx! Looks like you're getting the hang of it! Keep at it and keep posting. I hope this is clear and works for you,
Barton

should be:
Expand|Select|Wrap|Line Numbers
  1. class TestFrame(wx.Frame): 
  2.     def __init__(self, parent, log): 
  3.  
  4.  
  5.         wx.Frame.__init__(self, parent, -1, "Dex Tracker Sco Editor", size=(640,480)) 
  6.         p = wx.Panel(self, -1, style=0) 
  7.         self.grid = WordGrid(p, log) 
  8.  
  9. ## ...
  10.  
  11.  
  12.         bs.Add(self.grid, 1, wx.GROW|wx.ALL, 5)
  13.  
Here, self refers to the frame and since
Expand|Select|Wrap|Line Numbers
  1.     def OnButton(self, evt): 
  2.         print "button selected" 
  3.         self.grid.savefile() 
  4.  
is a method of the frame, self is the frame here too.
Oct 31 '06 #2
that seems to work as far as running the program. I seem to be able to change the cell values but the changes are not saved in the file. What is happening is that I get an exact (except it does get rid of the whitespace) copy of what I read but none of my changes (I lost 20k of whitespace and god knows what else invisible charecters). I have tried to read them off the screen but I am getting nowhere with that.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import wx
  3. import wx.grid as gridlib
  4. import sys
  5.  
  6.  
  7.  
  8. #---------------------------------------------------------------------------
  9.  
  10. class WordGrid(gridlib.Grid):
  11.  
  12.     def __init__(self, parent, log):
  13.         gridlib.Grid.__init__(self, parent, -1)
  14.         self.loadFile()
  15.  
  16.         self.CreateGrid(len(self.rows), self.widestRow)
  17.  
  18.         for r, row in enumerate(self.rows):
  19.             for c, col in enumerate(row):
  20.                 self.SetCellValue(r, c, col)
  21.             self.SetColSize(c, 10*self.widestCol)
  22.  
  23.         for c, label in enumerate(self.header):
  24.             self.SetColLabelValue(c, label)
  25.  
  26.         self.Bind(gridlib.EVT_GRID_CELL_CHANGE, self.OnCellChange)
  27.  
  28.  
  29.     def OnCellChange(self, evt):
  30.         row = self.GetGridCursorRow()
  31.         col = self.GetGridCursorCol()
  32.  
  33.         #self.SetCellValue(evt.GetRow(), evt.GetPosition(), self.GetValue(row, col))
  34.         #This is the example no good will keep it in the same position
  35.  
  36.         if value == 'no good':
  37.             self.moveTo = evt.GetRow(), evt.GetCol()   
  38.  
  39.     def loadFile(self):
  40.        #from_file 
  41.        infile = open(sys.argv[1], 'r') #The first argument passed in is the file name
  42.        foundHeader = False
  43.        self.rows = []
  44.        for line in infile:
  45.            if sys.argv[2] in line: #look for the second argument and make that the header
  46.                #removefirst = line.split(' ')
  47.                self.header = line.split()
  48.                #foundHeader = 'true'
  49.                continue     # we don't want to process this line any further
  50.            else:
  51.                self.rows.append(line.split())
  52.  
  53.        self.widestRow = max([len(r) for r in self.rows])
  54.        self.widestCol = max([len(c) for c in [r for r in self.rows]])
  55.  
  56.     def savefile(self):
  57.         linestring = ''
  58.         outfile = open(sys.argv[1], 'w') #open the file defined in the output line for writing
  59.         for word in self.header:
  60.             linestring = linestring + word + ' '
  61.         outfile.write(linestring + '\n')
  62.         linestring = ''
  63.         for row in self.rows:
  64.             for word in row:
  65.               linestring = linestring + ' ' + word
  66.             outfile.write(linestring)
  67.             outfile.write('\n')
  68.             linestring = ''
  69.         print('this is a test to see if I can Crash it')      
  70.  
  71. class TestFrame(wx.Frame):
  72.     def __init__(self, parent, log):
  73.  
  74.         wx.Frame.__init__(self, parent, -1, "Dex Tracker Sco Editor", size=(640,480))
  75.         p = wx.Panel(self, -1, style=0)
  76.         self.grid = WordGrid(p, log)
  77.         #grid = CustTableGrid(p, log)
  78.         b = wx.Button(p, -1, "Save Grid")
  79.         b.SetDefault()
  80.         self.Bind(wx.EVT_BUTTON, self.OnButton, b)
  81.         b.Bind(wx.EVT_SET_FOCUS, self.OnButtonFocus)
  82.         bs = wx.BoxSizer(wx.VERTICAL)
  83.         bs.Add(self.grid, 1, wx.GROW|wx.ALL, 5)
  84.         bs.Add(b)
  85.         p.SetSizer(bs)
  86.  
  87.     def OnButton(self, evt):
  88.         print "button selected"
  89.         self.grid.savefile()
  90.         #WordGrid(p, log).savefile(self)
  91.         #grid = WordGrid(self, log).savefile()
  92.         #self.WordGrid.savefile(self)
  93.  
  94.     def OnButtonFocus(self, evt):
  95.         print "button focus"
  96.  
  97. #---------------------------------------------------------------------------
  98. #def main():
  99.  
  100. def main(From_File, find_string):
  101.     """This is the entire editor for .sco files..  It doesn't realy care if it is music or not.  Any file that you lay out with even rows and collums 
  102.     can be displayed  The first argument passed to main is the file to be used and the second if the string to be used as the command to set up the header of the grid.
  103.     The sting you wish to use to identify the header should be placed last so it doesn't show up in the grid.
  104.     """
  105.  
  106.     import sys
  107.  
  108.     app = wx.PySimpleApp()
  109.     frame = TestFrame(None, sys.stdout)
  110.     frame.Show(True)
  111.     app.MainLoop()
  112.     pass
  113.  
  114. if __name__ == '__main__':
  115.     import sys
  116.     #try: 
  117.     main(sys.argv[1], sys.argv[2])
  118.  
  119.  
Nov 1 '06 #3
bartonc
6,596 Expert 4TB
It looks like you are trying to do something like this:
Expand|Select|Wrap|Line Numbers
  1.         #self.SetCellValue(evt.GetRow(), evt.GetPosition(), self.GetValue(row, col))
  2.  
  3.         self.rows[row][col] = self.GetValue(row, col)
  4.  
Use lots of print statements when debugging, like
Expand|Select|Wrap|Line Numbers
  1.         value = self.GetValue(row, col)
  2.         print value, type(value)
  3.  
to make sure that the wx function returns the type that you need. Sometimes it's a good idea to use
Expand|Select|Wrap|Line Numbers
  1.         self.rows[row][col] = str(self.GetValue(row, col))
  2.  
just in case it's unicode or wxStr object.
Nov 1 '06 #4
That worked fantastic thanks alot.
Nov 1 '06 #5
bartonc
6,596 Expert 4TB
That worked fantastic thanks alot.
You are quite welcome!
Nov 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Blue® | last post by:
I found this piece of JS which serves exactly what I need. It validates if the input is exactly five numberic characters. When it is validated, the form action is not carried out. What should I...
5
by: Steve | last post by:
Visual Studio 2003 .Net / C# I have a 2 page Tab Control for users to add a Job in my application. The first page is for them to choose which type of Job they would like. The type of Job...
18
by: Geoff Cox | last post by:
Hello, I am trying to print out the array values for a second time but get error on page message? Thanks Geoff <html>
2
by: Richard Lionheart | last post by:
Hi All, I generated a WebForm and created a Virtual Directory for it IIS. But I got an error message (shown below) saying something like my app lacked appropriate privileges. David Wang...
19
by: Mountain | last post by:
I would like opinions on whether this solution could be important and whether anyone knows if it is already solved. (I would also like to know if anyone thinks it cannot be solved.) First, we...
1
by: beachboy | last post by:
My project has many class (function), it need to create a new instance each time by the id value, how can i write a code as much simple? Actually, all cases is doing the same thing.... , but is...
6
by: Jeff | last post by:
Hey (and thank you for reading my post) In visual web developer 2005 express edition I've created a simple website project.. At this website I want users who register to be able to upload a...
3
by: Miro | last post by:
Im really stuck on something. My goal is to get away from Image Lists and to use a Resource file instead. I followed an example and went to Properties of my project and clicked on the resource...
4
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi All, Our company is merging two production domains(from two different servers) onto one server. So during the migration we use an intranet testing server to hold all files from these two...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.