How can create widgets in for loop and change its value

Post here if you need help with creating a Graphical User Interface in Python.

How can create widgets in for loop and change its value

Postby John R » Fri Jul 04, 2014 9:21 am

Dear All,

I am trying to create a Tkinter python text widgets using for loop and that is fine but unable to refer or
change the values in it from another function. How can I do this? Somebody please help me.

While I was defining the text widget, for giving different names to each text widget, I added
different string names along with for loop count and that concatenated string name later assigned
for each new text widget creation. This will be good for creating all the text boxes within few lines
of code but when I called the concatenated widget name for value changes, it throws error like
below

App instance has no attribute 'txtName1'

Please see my code snippet for reference

Code: Select all

import os
from Tkinter import *
import Tkinter as tk
import ttk
import tkFont

class App():

    def __init__ (self):
   
        root['width']=700
        root['height']=600
      
        btnChange = tk.Button(root,width=10, height=1,text="Change",relief=FLAT,bg="gray",
                             command=lambda: self.FcnValChange())
        btnChange.pack()
        btnChange.place( x=280, y = 25)       

    def fcnControlAdd(self):
        try:
            self.TopLevel1 = Toplevel(root,bg='white',height=500, width=380)      
            txtNameGroup="txtEmpNo"
            txtPlaceY=0               
                     
            for Q in range(10):                     
              txtName="self."+txtNameGroup+str(Q) # !!!           
              if Q == 0:
                  txtPlaceY=40
              else:
                  txtPlaceY=txtPlaceY+40                         
              txtName= Text(self.TopLevel1,height=1, width=10,borderwidth = 2)             
              txtName.insert(INSERT,"NA")
              txtName.pack()                     
              txtName.place(x=200, y = txtPlaceY)           
             

            btnCancel = tk.Button(self.TopLevel1,width=10, height=1,text="Cancel",relief=FLAT,bg="gray")                                 
            btnCancel.pack()
            btnCancel.place( x=220, y = 450)
           
        except IOError:
           print "Error: Control Addition error"
         
    def FcnValChange(self):       
         
          self.txtName1.insert(INSERT,"Values Changed")  #Error!!!!!!!!!!!,  App instance has no attribute 'txtName1' !!!!!   
          data=self.txtName1.get(1.0,END)[:-1]

root=tk.Tk()
app = App()
root.mainloop()




I am using Tkinter Python 2.6

Thanks in advance
Last edited by stranac on Fri Jul 04, 2014 10:15 am, edited 1 time in total.
Reason: First post lock.
John R
 
Posts: 13
Joined: Fri Jul 04, 2014 6:41 am

Re: How can create widgets in for loop and change its value

Postby stranac » Fri Jul 04, 2014 10:44 am

That!s not how names work in python:
Code: Select all
>>> name = 'x'
>>> name = 3
>>> name
3
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

I would suggest using a dict:
Code: Select all
>>> name = 'x'
>>> widgets = {}
>>> widgets[name] = 3
>>> widgets['x']
3
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: How can create widgets in for loop and change its value

Postby John R » Fri Jul 04, 2014 11:42 am

Dear stranac,

Thank you very much for the very useful reply, let me try this option.
John R
 
Posts: 13
Joined: Fri Jul 04, 2014 6:41 am

Re: How can create widgets in for loop and change its value

Postby John R » Mon Jul 07, 2014 4:30 am

Dear stranac,

Thanks again the code works well, it is the right answer,really appriciate you for the effort. Is there any way to mark this as correct answer,couldnot find any :roll:
John R
 
Posts: 13
Joined: Fri Jul 04, 2014 6:41 am


Return to GUI

Who is online

Users browsing this forum: No registered users and 1 guest