1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Rep Power

    Python tk scoping problems

    I am having problems with scoping using tkinter. I am trying to encapsulate everything into classes to neatly organize my code, but when I create things like tables and frames, I can't access them in other classes.

    I have too much code to post it all, but for example when I have:

    from tkinter import *
    from tkinter.ttk import *
    from tkinter.filedialog import askopenfilename
    import re
    import os
    class RootWindow:
        # Class constructor
        def __init__(self, root):
        def CreateTable(self, root):
            table_scrollbar = Scrollbar(root, orient=VERTICAL)
            table = Treeview(root, columns=['a', 'b', 'c', 'd', 'e', 'f', 'g'],
            show='headings', yscrollcommand=table_scrollbar.set)
            table.column('A', width=100, anchor='center')
            table.column('B', width=100, anchor='center')
            table.column('C', width=100, anchor='center')
            table.column('D', width=120, anchor='center')
            table.column('E', width=100, anchor='center')
            table.column('F', width=100, anchor='center')
            table.column('G', width=100, anchor='center')
            table.heading('a', text='A')
            table.heading('b', text='B')
            table.heading('c', text='C')
            table.heading('d', text='D')
            table.heading('e', text='E')
            table.heading('f', text='F')
            table.heading('g', text='G')
            table_scrollbar.pack(side=RIGHT, fill=Y)
            table.pack(side=RIGHT, fill=Y)
    So later on if I make a class that loads data into the table, I am unable to access the table. I'm having the same problems with frames. I don't want to make a bunch of global frames to put labels and such on.

    What is the best way to make sure my classes and methods can see each other?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Rep Power
    First of all, why do you have an __init__ method that does absolutely nothing except call another method with the exact same parameters it gets? Why not, uh, just call the other method __init__.

    Second: if you want something to be accessible outside a function, you need to have a reference to it outside function scope. Just add a "self.table = table" at the end of that method.

IMN logo majestic logo threadwatch logo seochat tools logo