Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

apply frame from external file in python 3.x?

Is there any way to apply a frame from external module to a root window. i have 2 files: in a.py which has the line self.root = root() and i have a function in a class which imports b.py and in b.py i have a class and instantiated a Frame to display in root window self.frame = Frame(root). There is no error but frame widget is not showing up in the root window. I tried changing root to self.root in b.py file

For Example:

#file 'a'
class Root:
    def __init__(self):
        self.root = root()
        root.title('Hello')
        self.b = None
    def boo(self):
        import b
        self.b = b.A()
Root.boo()

and

#file 'b'
class A:
    def __init__(self):
        self.frame = tk.LabelFrame(self.root)
        self.frame.pack()
    def __a_meth__(self):
        Button(self.frame, text = 'YES')
        Button.pack()

What changes need to be done?

question from:https://stackoverflow.com/questions/65661414/apply-frame-from-external-file-in-python-3-x

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Generally you would pass in anything needed:

#file 'a'
class Root:
    def __init__(self):
        self.root = root()
        self.root.title('Hello')
        self.b = None
    def boo(self):
        import b
        self.b = b.A(self.root) # pass the root object in
        self.b.__a_meth__() # don't forget to call this if you want to see anything
Root.boo()

#file 'b'
class A:
    def __init__(self, root):
        self.root = root
        self.frame = tk.LabelFrame(self.root)
        self.frame.pack()
    def __a_meth__(self):
        Button(self.frame, text = 'YES')
        Button.pack()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...