[Tutor] tkinter nested class question

Elwin Estle chrysalis_reborn at yahoo.com
Tue Feb 1 22:05:10 CET 2011


...at least I think it would be nested.

Anyway, I have several "widget groups" that I want to create inside a frame, which is inside a class.  I started to just do them with copying and pasting, and changing the values for each one (the first one is commented out, I left it in so you could see what I am after), but thought maybe I could make a class for one widget group.

...and it works, sort of.

When I call the widget group class inside the main class, it shows up, but appears to be using the root window instead of the frame, which doesn't surprise me, since there is no connection between the widget group and the frame I want them to appear in.  But the question is, how to make that connection?

Here is the code.  I want "self.rev_stuff" to show up inside self.ck_header_frame.  Am I right in guessing that it has to do either with the Header_item class being an object, or I need to do something in its def __init__ ?

from Tkinter import *

class Header_item(object):
    
    def __init__(self, lab_text = '', lab_width = 5, ent_width = 5, grid_row = 1, grid_col = 1):
        self.container = Frame()
        self.info_label = Label(self.container, text = lab_text, width = lab_width)
        self.info_ent = Entry(self.container, width = ent_width)
        self.info_label.pack(side = 'left')
        self.info_ent.pack(side = 'left')
        self.container.grid(row = grid_row, column = grid_col)
   

class Checksheet(Frame):
    
    def __init__(self, master, rows):
        
        rows += 2
    
        self.ck_header_frame = Frame()
        self.ck_header_frame.grid(row = 1, column = 1, padx = 5, pady = 5)
        
        self.feature_block = Frame()
        self.feature_block.grid(row = 2, column = 1, padx = 5, pady = 5)
        
        """
        self.f_rev_frame = Frame(self.ck_header_frame)      
        self.f_rev_lab = Label(self.ck_header_frame, text = 'FORM REV:', width = 12)
        self.f_rev_ent = Entry(self.ck_header_frame, width = 12)
        self.f_rev_lab.pack(side = 'left')
        self.f_rev_ent.pack(side = 'left')
        self.f_rev_frame.grid(row = 1, column = 2)
        """
        self.rev_stuff = Header_item(lab_text = 'FORM REV:',
                                     lab_width = 12,
                                     ent_width = 12,
                                     grid_row = 1,
                                     grid_col = 2)
        
        features_header = []
        features_header.append(('NO.', 5))
        features_header.append(('CofC', 5))
        features_header.append(('FEATURE DESCRIPTION', 50))
        features_header.append(('FREQ.', 12))
        features_header.append(('GAGES', 12))
        
        column_count = 1
        
        self.feat_hdr = dict()
        self.feat_num = dict()
        self.feat_class = dict()
        self.feat_desc = dict()
        self.feat_freq = dict()
        self.feat_gages = dict()
                
        for _column, item in enumerate(features_header):
            _column += 1
            col_name, _width = item
            self.feat_hdr[_column] = Label(self.feature_block, text = col_name, width = _width, relief = 'groove')
            self.feat_hdr[_column].grid(row = 1, column = _column)
        
        for col_name, _width in features_header:
            if col_name == 'NO.':
                for i in range(2, rows):
                    self.feat_num[i] = Entry(self.feature_block, width = _width, relief = 'sunken')
                    self.feat_num[i].grid(row = i, column = 1)
            elif col_name == 'CofC':
                for i in range(2, rows):
                    self.feat_class[i] = Entry(self.feature_block, width = _width, relief = 'sunken')
                    self.feat_class[i].grid(row = i, column = 2)
            elif col_name == 'FEATURE DESCRIPTION':
                for i in range(2, rows):
                    self.feat_desc[i] = Entry(self.feature_block, width = _width, relief = 'sunken')
                    self.feat_desc[i].grid(row = i, column = 3)
            elif col_name == 'FREQ.':
                for i in range(2, rows):
                    self.feat_freq[i] = Entry(self.feature_block, width = _width, relief = 'sunken')
                    self.feat_freq[i].grid(row = i, column = 4)
            elif col_name == 'GAGES':
                for i in range(2, rows):
                    self.feat_gages[i] = Entry(self.feature_block, width = _width, relief = 'sunken')
                    self.feat_gages[i].grid(row = i, column = 5)


root = Tk()
checksheet = Checksheet(root, 5)
root.mainloop()


      


More information about the Tutor mailing list