TKinter, Entry objects and dynamic naming

I

Ian Vincent

I am hoping someone may be able to help.

I am using Python and TKinter to create a GUI program that will
eventually create an XML file for a project I am working on. Now, the XML
file contents changes depending on the type of file it represents - so I
am dynamically creating the TK Entry boxes. The problem is, I want the
variable assigned to each of these boxes to be dynamically assigned as
well but I cannot see how to do this (if it is indeed possible).

Example

If the file the XML is to represent is a spreadsheet - I want the first
entry box to be:

spreadsheetField = StringVar()
value = Entry(root, relief=SUNKEN, justify=LEFT,
textvariable=spreadsheetField)

however, if the file is a document - I want the first entry box to be:

documentField = StringVar()
value = Entry(root, relief=SUNKEN, justify=LEFT,
textvariable=documentField)

I have quite a few Entry boxes for each file type (about 10) and a lot of
filetypes (over 80), so I do not want to have a custom function for each
filetype if I can help it.

I have looked at using a dictionary but I cannot get my head around how
to do that either.

If anyone can give me any ideas, I would be very grateful.
 
E

Eric Brunel

I am hoping someone may be able to help.

I am using Python and TKinter to create a GUI program that will
eventually create an XML file for a project I am working on. Now, the XML
file contents changes depending on the type of file it represents - so I
am dynamically creating the TK Entry boxes. The problem is, I want the
variable assigned to each of these boxes to be dynamically assigned as
well but I cannot see how to do this (if it is indeed possible).

Example

If the file the XML is to represent is a spreadsheet - I want the first
entry box to be:

spreadsheetField = StringVar()
value = Entry(root, relief=SUNKEN, justify=LEFT,
textvariable=spreadsheetField)

however, if the file is a document - I want the first entry box to be:

documentField = StringVar()
value = Entry(root, relief=SUNKEN, justify=LEFT,
textvariable=documentField)

I have quite a few Entry boxes for each file type (about 10) and a lot of
filetypes (over 80), so I do not want to have a custom function for each
filetype if I can help it.

Your requirements are not very precise here. What do represent the entry fields? How do you build them? Are they always strings or can they be integers, or floats, or anything else? Do you have only entry fields or other widgets as well (check buttons, radio buttons, ...)? How do you know the format of the dialog you build?
I have looked at using a dictionary but I cannot get my head around how
to do that either.

This is the way I would go, but without answering the questions above, I won't be able to give you much advice. Supposing you have only entry fields accepting strings, a mapping file type -> field name -> StringVar may be a good solution.

HTH
 
I

Ian Vincent

Ian Vincent said:
I have looked at using a dictionary but I cannot get my head around
how to do that either.

I have tried this now but the Entry field just does not seem to work:

def add_variable(self, root, varname):
Label(root, text=varname + ': ').grid(row=self.row, column=0,
sticky=E)
self.properties[varname] = StringVar()
value = Entry(root, relief=SUNKEN, justify=LEFT,
textvariable=self.properties[varname])
value.grid(row=self.row, column=1, sticky=E+W)
self.row = self.row + 1
return value


Confused. :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top