By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,942 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,942 IT Pros & Developers. It's quick & easy.

TKinter, Entry objects and dynamic naming

P: n/a
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.
Sep 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 13 Sep 2005 08:51:47 +0100, Ian Vincent <i_*******@hotmail.com> wrote:
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
--
python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"
Sep 13 '05 #2

P: n/a
Ian Vincent <i_*******@hotmail.com> wrote in
news:Xn********************************@10.15.188. 42:

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. :-(
Sep 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.