[Tkinter-discuss] Storing structured, 'widget-specific-data', then loading it back in la ter

Chris Nethery clnethery at juno.com
Sun Oct 3 05:32:06 CEST 2004


Hello everyone!

I have been working on the following for a while and just cannot get it to work correctly, so I am hoping that someone could offer some advice.

I am trying to create a group of (re-usable) methods that take user input data and store it for future reference, then reload it later to the same widget where it was originally created.  I wrote the following program to try to get the process working.  The first button is supposed to store the data, and the second button is meant to plug the data back into the widget.  I seem to be having difficulty plugging the data back into the widget, because I cannot identify it(the widget) properly.  Any thoughts?

Thanking you in advance,


Chris Nethery



import marshal
import Tkinter
import Pmw

root = Tkinter.Tk()

screenWidth, screenHeight = root.maxsize()
screenSize = "%dx%d" % (screenWidth, screenHeight)
root.geometry(screenSize)
Pmw.initialise(root)


class DataManagement:

    def __init__(self,actualWidgetName,frame,widget,data):
        self.actualWidgetName = actualWidgetName
        self.f = frame

        collectedList = []
        self.cL = collectedList
        self.widget = widget
        self.data = data
        self.dataStructure = [self.widget,self.data]
        if self.dataStructure not in self.cL:
            self.cL.append(self.dataStructure)
            file = open(self.widget + '.ats','wb')
            bytes = marshal.dump(self.dataStructure,file)
            file.close()
        self.testButton = Tkinter.Button(self.f, font=(('Verdana'), '7'), text='Test', command=self.getStoredData(self.widget))
        self.testButton.place(relx=0.41,rely=0.01,anchor=Tkinter.NW)


    def getStoredData(self,widget):
        self.storeFile = open(widget + '.ats','rb')
        unmarshal = marshal.load(self.storeFile)
        self.unMarsheledData = unmarshal
        self.storeFile.close()
        self.outPutDataInNewWidget(self.unMarsheledData,self.widget,self.actualWidgetName)

    def outPutDataInNewWidget(self,unMarsheledData,widget,actualWidgetName):
        print unMarsheledData[0]
        print actualWidgetName['CB']
        if unMarsheledData[0] == widget:
            actualWidgetName['CB'].selectitem(unMarsheledData[0], setentry = 1)
        else:
            print 'unMarsheledData[0] != fileName'
#        pass

        
class CreateWidgets:

    def __init__(self,root):

        self.root = root

        self.mainFrame = Tkinter.Frame(self.root, relief=Tkinter.GROOVE, borderwidth=1, width=900, height=450)
        self.mainFrame.place(relx=0.01,rely=0.01,anchor=Tkinter.NW)

        stuff = ['1','2','3','4','5','6','7','8','9','10']
        self.comboBox = Pmw.ComboBox(
		        self.mainFrame,
                        listbox_width=26,
                        history=0,
                        unique=0,
                        dropdown=0,
                        listbox_height=10,
	        	scrolledlist_items=stuff,
	                )
        self.comboBox.place(relx=0.01,rely=0.01,anchor=Tkinter.NW)

        frameHash = {self.comboBox:self.mainFrame}

# Add more widgets later
        for item in [
            (self.comboBox),
            ]:
            self.frame = frameHash[item]
            self.widget = str(item)
            self.data = item.component('entryfield').component('entry').get()
            self.actualWidgetName = {'CB':self.comboBox}


        self.saveButton = Tkinter.Button(self.mainFrame, font=(('Verdana'), '7'), text='Save', command=self.goStoreTheData)
        self.saveButton.place(relx=0.21,rely=0.01,anchor=Tkinter.NW)

    def goStoreTheData(self):
        DataManagement(self.actualWidgetName,self.mainFrame,self.widget,self.data)



if __name__ == '__main__':
    W =  CreateWidgets(root)
    root.mainloop()


More information about the Tkinter-discuss mailing list