python - for loop to insert things into a tkinter window -
i have tkinter program has add significant amount of data window tried write loop take care of since have use string variable name of object tkinter running .insert() on object. didn't explain here method
def fillwindow(self): global filedirectory location = os.path.join(filedirectory, family + '.txt') file = open(location, 'r') ordersdict = {} line in file: (key, value) = line.split(':', 1) ordersdict[key] = value key in ordersdict: ordersdict[key] = ordersdict[key][:-2] item in ordersdict: if item[0] == '#': if item[1] == 'o': name = 'ordered%s' %item[2:] right here problem line because have variable matches name of entry object created 'name' string variable gives me error "attributeerror: 'str' object has no attribute 'insert'"
name.insert(0,ordersdict[item]) here entire class. makes tkinter window , fills sort of shipping screen entries how many orders of thing needed. i'm new know things long way lot.
class editshippingwindow(tkinter.toplevel): def __init__(self, student): tkinter.toplevel.__init__(self) self.title('orders') family = student ## window filling agegrouplabel = tkinter.label(self,text='age group') agegrouplabel.grid(row=0,column=0) itemcolumnlabel = tkinter.label(self,text='item') itemcolumnlabel.grid(row=0, column=1) costcolumnlabel = tkinter.label(self,text='cost') costcolumnlabel.grid(row=0, column=2) orderedcolumnlabel = tkinter.label(self,text='ordered') orderedcolumnlabel.grid(row=0, column=3) paidcolumnlabel = tkinter.label(self,text='paid') paidcolumnlabel.grid(row=0, column=4) receivedcolumnlabel = tkinter.label(self,text='received') receivedcolumnlabel.grid(row=0, column=5) #item filling column1list = ['t-shirt (2t):$9.00', 't-shirt (3t):$9.00', 't-shirt (4t):$9.00', 'praise music cd:$10.00', ':', 'vest l(size 6):$10.00', 'vest xl(size 8):$10.00', 'hand book (kj/niv):$8.75', 'handbook bag:$6.00', 'memory cd (kj/niv):$10.00', ':', 'vest l(size 10):$10.00', 'vest xl(size 12):$10.00', 'hand glider (kj/niv/nkj):$10.00', 'wing runner (kj/niv/nkj):$10.00', 'sky stormer (kj/niv/nkj):$10.00', 'handbook bag:$5.00', 'memory cd (s/h/c):$10.00', 'hand glider freq. flyer:$8.00', 'wing runner freq. flyer:$8.00', 'sky stormer handbook:$8.00' , ':', 'uniform t-shirt size (10/12/14):$13.00', 'uniform t-shirt size(10/12/14):$13.00', 'uniform t-shirt(adult s / m / l / xl):$13.00', '3rd & 4th gr. book 1 (kj / niv / nkj):$8.75', '3rd & 4th gr. book 2 (kj / niv / nkj):$8.75', '4th & 5th gr. book 1 (kj / niv / nkj):$8.75', '4th & 5th gr. book 2 (kj / niv / nkj):$8.75', 'memory cd 3rd & 4th gr. book (1/2):$10.00', 'drawstring backpack:$5.50'] column1num = 1 item in column1list: num = str(column1num) (title, price) = item.split(':') objectname1 = 'column1row' + num objectname1 = tkinter.label(self,text=title) objectname1.grid(row=column1num, column=1) objectname2 = 'column1row' + num objectname2 = tkinter.label(self,text=price) objectname2.grid(row=column1num, column=2) column1num += 1 #ordered paid recieved filler in range(32): if == 11 or == 22 or == 0 or == 5: pass else: width = 10 # first column title1 = 'ordered' + str(i) self.title1 = tkinter.entry(self,width=width) self.title1.grid(row=i,column=3) #self.title1.insert(0, title1) #second title2 = 'paid' + str(i) self.title2 = tkinter.entry(self,width=width) self.title2.grid(row=i,column=4) #self.title2.insert(0, title2) #third title3 = 'received' + str(i) self.title3 = tkinter.entry(self,width=width) self.title3.grid(row=i,column=5) #self.title3.insert(0, title3) ## methods def fillwindow(self): global filedirectory location = os.path.join(filedirectory, family + '.txt') file = open(location, 'r') ordersdict = {} line in file: (key, value) = line.split(':', 1) ordersdict[key] = value key in ordersdict: ordersdict[key] = ordersdict[key][:-2] item in ordersdict: if item[0] == '#': if item[1] == 'o': self.name = 'ordered%s' %item[2:] self.name.insert(0,ordersdict[item]) fillwindow(self)
it looks have conceptual error there: inside method, variable "name" not exist last line on first listing. created, , points ordinary python string -- if using "name" variable elsewhere on class variable not exist inside method.
for easy fix of existing code, try calling variable "self.name" instead of name created, , on last line in method use: self.name.insert(0,ordersdict[item]) instead.
the self. prefix turn variable instance variable, shared across methods on same instance of class.
on side note, don' t need dictionary less 3 consecutive loops on method, insert relevant values extract "line" in text variable.
Comments
Post a Comment