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

    Join Date
    Oct 2017
    Posts
    82
    Rep Power
    1

    [Python 3 + Tkinter] Classes Scripts Multiple Windows not Works..., help needed!


    Hey Python Programmers and GUI Developers...

    I get two scripts writted in python 3 and tkinter....
    I try to make multiple windows, but it don't works and i get an error...,

    So this is my first script (The run script):
    Code:
    from tkinter import *
    
    class Window(Frame):
    
        def __init__(self, master=None):
    
            self.master = master
    
            Frame.__init__(self, master)
    
            self.init_window()
    
        def init_window(self):
    
            self.master.title("MenuBar")
    
            menu = Menu(self.master)
            self.master.config(menu = menu)
    
            filemenu = Menu(menu)
            filemenu.add_command(label = "Open", command = self.Open)
            menu.add_cascade(label = "File", menu = filemenu)
    
        def Open(self):
    
            import scripts.maxest
    
            scripts.maxest.Starter()
    
    
    
    
    
    root = Tk()
    Window(root)
    root.mainloop()

    And this is the "import scripts.maxest" script, found in my map 'scripts'...
    So i get an run script (with the root window) and an scripts.maxest script writted...

    This is the maxest script, in the scripts folder:
    Code:
    from tkinter import *
    
    class Starter(Frame):
    
        def __init__(self, getter=None):
    
            self.getter = getter
    
            Frame.__init__(self, getter)
    
            self.init_window()
    
        def init_window(self):
    
            self.getter.title("Open new File")
    
            b1 = Button(self.getter, text = "Get It")
            b2 = Button(self.getter, text = "Made")
            b3 = Button(self.getter, text = "Dass")
    
            b1.pack()
            b2.pack()
            b3.pack()
    
    winer = Toplevel(root)
    Starter(winer)
    winer.mainloop()
    So if i run my first script (or rether the run script)..., i get this error:
    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
    File "C:\Users\Gebruiker\Desktop\MenuBar\Tkinter MenuBar.py", line 26, in Open
    import scripts.maxest
    File "C:\Users\Gebruiker\Desktop\MenuBar\scripts\maxest.py", line 25, in <module>
    winer = Toplevel(root)
    NameError: name 'root' is not defined

    Whats wrong with my code...., i think thad this error are in the maxest script,
    it said thad 'root' is not defined...

    Can anyone correct my code, just i can expiriment with it ?.....
    Thanks for help, Jamie.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,967
    Rep Power
    509
    You'll need to learn variable scope correctly. "global" is the highest level of scope checking. "global" means "module global". global variables are not directly accessible as global variables in another module.

    This is a way to transfer create a global variable in another module.

    In the first module
    Code:
    import b
    
    root = 'something'
    b.root = root
    And this cannot work as you have written your secondary module because it needs the value of root to successfully import. So had you tried

    import b
    b.root = root

    would have failed. The import statement fails before python evaluates the b.root = root statement.

    If you were to define in the second module
    Code:
    def main(ROOT):
        global root
        root = ROOT
        winer = Toplevel(root)
        Starter(winer)
        winer.mainloop()
    and then in your first module used
    Code:
        def Open(self):
            r.main(root)
            r.Starter()
    you might get something to work a little better. I was able to get your open method to pop up another few windows from the slave module.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    82
    Rep Power
    1
    Originally Posted by b49P23TIvg
    You'll need to learn variable scope correctly. "global" is the highest level of scope checking. "global" means "module global". global variables are not directly accessible as global variables in another module.

    This is a way to transfer create a global variable in another module.

    In the first module
    Code:
    import b
    
    root = 'something'
    b.root = root
    And this cannot work as you have written your secondary module because it needs the value of root to successfully import. So had you tried

    import b
    b.root = root

    would have failed. The import statement fails before python evaluates the b.root = root statement.

    If you were to define in the second module
    Code:
    def main(ROOT):
        global root
        root = ROOT
        winer = Toplevel(root)
        Starter(winer)
        winer.mainloop()
    and then in your first module used
    Code:
        def Open(self):
            r.main(root)
            r.Starter()
    you might get something to work a little better. I was able to get your open method to pop up another few windows from the slave module.
    OK..., but can you put your example in my code please, so i can learn
    excacly to use this method...

    Now I don't know what you means... So if you put your example in
    my code, to correct my code, I hope thad I can learn your method
    to understand it better. ..

    Thanks for help, Jamie...
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,967
    Rep Power
    509
    Your approach to multiple windows in this forum thread is a bad idea. dwblas said to use the Toplevel "widget" for making multiple windows. It is good advice.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo